思路就是存数据,怎么大都不再怕啦!
#include "stdio.h"
#include "string.h"
char m1[1001];
char m2[1001];
int BigAdd(int a, int b){
printf("a:%d,b:%d\n",a,b);
sprintf(m1, "%d", a);
sprintf(m2, "%d", b);
int len1 = strlen(m1);
int len2 = strlen(m2);
char sum[1003];
for(int i = 0; i < sizeof(sum)/sizeof(*sum); ++i){
sum[i] = '0';
}
--len1;
--len2;
int sumIndex = 1002;
int flag = 0;
while(len1 >= 0 && len2 >= 0){
int temp = m1[len1]-'0' + m2[len2]-'0'+flag;
flag = temp/10;
if(flag > 0){
temp %= 10;
}
sum[sumIndex] = temp + '0';
--len1;
--len2;
--sumIndex;
}
while(len1 >= 0){
int temp = m1[len1]-'0'+flag;
flag = temp/10;
if(flag > 0){
temp %= 10;
}
sum[sumIndex] = temp + '0';
--len1;
--sumIndex;
}
while(len2 >= 0){
int temp = m2[len2]-'0'+flag;
flag = temp/10;
if(flag > 0){
temp %= 10;
}
sum[sumIndex] = temp + '0';
--len2;
--sumIndex;
}
if(flag == 1){
sum[sumIndex] = flag + '0';
--sumIndex;
}
for(int i = sumIndex+1; i < 1003; ++i){
printf("%c",sum[i]);
}
printf("\n");
}
int main(){
BigAdd(100001111,999999999);
}
PS:今天老大提到的一个算法。我顺便就实现了。