145 条题解
-
-7
水一篇题解哈哈哈哈哈
声明:为了不错误引导新手上路,我会区分新手和神犇的解题方法(当然神犇不是我)
新手请看这里:
#include<iostream> using namespace std; int main() { int a,b; //定义a,b两数,数据规模巨大的话用long long int也不是不可~ cin>>a>>b; //输入a,b cout<<a+b; //输出a+b的结果,等同于int c=a+b; cout<<c; return 0; //return 0好习惯 }
神犇请看这里:(高精度加法)
先用字符串数组,以字符形式储存两个大数。再依次储存被加数,加数,和。之后再将字符数字转为四位一块的整数数字,逐块相加,并进位,最后计算和的块的总数即可。
#include <bits/stdc++.h> #define N 200 int Pow(int a, int b) { int i = 0, result = 1; for(i = 0; i < b; ++i) { result *= a; } return result; } int main() { char stra[N], strb[N]; int i = 0, step = 4, carry = 0; int lengtha, lengthb, maxlength, resultsize; int numa[N], numb[N],numc[N]; memset(numa, 0, sizeof(numa)); memset(numb, 0, sizeof(numb)); memset(numc, 0, sizeof(numc)); scanf("%s%s", stra, strb); lengtha = strlen(stra); lengthb = strlen(strb); for(i = lengtha-1; i >= 0; --i) { numa[(lengtha-1-i)/step] += (stra[i]-'0')*Pow(10,(lengtha-1-i)%step); } for(i = lengthb-1; i >= 0; --i) { numb[(lengthb-1-i)/step] += (strb[i]-'0')*Pow(10,(lengthb-1-i)%step); } maxlength = lengtha > lengthb ? lengtha : lengthb; for(i = 0; i <= maxlength/step; ++i) { numc[i] = (numa[i] + numb[i])%Pow(10, step) + carry; carry = (numa[i] + numb[i])/Pow(10, step); } resultsize = numc[maxlength/step] > 0 ? maxlength/step : maxlength/step - 1; printf("%d", numc[resultsize]); for(i = resultsize-1; i >= 0; --i) { printf("%04d", numc[i]); } printf("\n"); return 0; }
信息
- ID
- 56
- 时间
- 1000ms
- 内存
- 1024MiB
- 难度
- 1
- 标签
- 递交数
- 9043
- 已通过
- 4027
- 上传者