高精度加法

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int a[240] = {0}, b[240] = {0}, c[240] = {0};
int i, ka, kb, k;
char al[240], bl[240];
gets(al);
ka = strlen(al);
gets(bl);
kb = strlen(bl);
if(ka >= kb) k = ka;
else k = kb;
for(i = 0;i < ka;i++) {
a[i] = al[ka-i-1]-'0';
}
for(i = 0;i < kb;i++) {
b[i] = bl[kb-i-1]-'0';
}
for(i = 0;i < k;i++) {
c[i] = a[i] + b[i] + c[i];
c[i+1] = c[i+1] + c[i]/10;
c[i] = c[i] % 10;
}
if(c[k]) k++;
for(i = k-1;i >= 0;i--) {
printf("%d", c[i]);
}
return 0;
}

posted @ 2018-01-20 10:19  thx666  阅读(72)  评论(0编辑  收藏  举报