【十二题解】 | #高精度整数加法#
高精度整数加法
http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include<stdio.h>
int main(){
char num1[1000]={0};
char num2[1000]={0};
while(scanf("%s", num1) != EOF){
scanf("%s", num2);
int len1, len2;
for(len1=0; num1[len1] != '\0'; len1++);
for(len2=0; num2[len2] != '\0'; len2++);
char ans[1000]={0};
int loc = 999;
int min=len1<len2?len1:len2;
int max=len1>len2?len1:len2;
int leijia=0;
int x=len1-1;
int y=len2-1;
for(; max>0; max--){
int sign =0;
int he;
if(x>=0 && y>=0){
he=num1[x]+num2[y]-'0'-'0'+leijia;
}
else if(x>=0){
he=num1[x]-'0'+leijia;
}
else{
he=num2[y]-'0'+leijia;
}
if(he>=10){
sign = 1;
he%=10;
}
ans[loc]=he+'0';
x--;
y--;
loc--;
leijia=sign==1?1:0;
}
if(leijia == 1){
ans[loc++]=leijia+'0';
}
int start;
for(start=0; ans[start]=='\0'; start++);
for(; start<1000; start++){
printf("%c", ans[start]);
}
printf("\n");
}
}