高精度加法
以下是高精度加法(两个加数,且都为正数,小于1000000位) 采取的是十进制来运算
#include <cstdio>
int a[1000000],b[1000000],c[1000000],la,lb,lc;
void _(){
for (int i=1; i<=la || i<=lb; i++){ //判断加完没
c[i]+=a[i]+b[i]; //把当前位加起来
c[i+1]+=c[i]/10; //进位
c[i]%=10; //当前位取模
if (lc<i+1) lc=i+1; //修改答案位数(这种方法修改有可能会多一位,在后面处理)
}
while (c[lc]==0) lc--;
}
int main(){
char ch=getchar();
while (ch<='0' || ch>'9') ch=getchar(); //把多余字符读掉(注意在'0'处用了"=",是为了防止前导零)
while (ch>='0' && ch<='9') a[++la]=ch-'0',ch=getchar(); //读入a
while (ch<='0' || ch>'9') ch=getchar(); //把多余字符读掉(注意在'0'处用了"=",是为了防止前导零)
while (ch>='0' && ch<='9') b[++lb]=ch-'0',ch=getchar(); //读入b
int t;
for (int i=1; i*2<=la; i++) t=a[i],a[i]=a[la-i+1],a[la-i+1]=t; //把a反过来
for (int i=1; i*2<=lb; i++) t=b[i],b[i]=b[lb-i+1],b[lb-i+1]=t; //把b反过来
_(); //运算
//printf("%d\n",lc);
for (int i=lc; i; i--) printf("%d",c[i]);
}