高精度加法

Constraints

Time Limit: 1 secs, Memory Limit: 8 MB

Description

 

Input A and B, output A+B

 

Input

 

Input two values, A and B.(0<=A,B<10^100)

 

Output

 

Output the result of A+B.

 

Sample Input

1234567890123456789

987654321

Sample Output

1234567891111111110

 

 

 

  1. #include<stdio.h>
  2. #include<string.h>
  3. void init(int*arr,int*len){
  4. int i;
  5. char st[101];
  6. scanf("%s", st);
  7. *len = strlen(st);
  8. for(i =0; i <*len; i++)
  9. arr[i]= st[*len -1- i]-'0';
  10. }
  11. int main(){
  12. int a[100], b[100], c[101], m, n, i, j, k, tmp;
  13. init(a,&n);
  14. init(b,&m);
  15. k = m > n ? m : n;
  16. for(i =0; i <= k; i++) c[i]=0;
  17. for(i = n; i < k; i++) a[i]=0;
  18. for(i = m; i < k; i++) b[i]=0;
  19. for(i =0; i < k; i++){
  20. tmp = a[i]+ b[i]+ c[i];
  21. c[i]= tmp %10;
  22. c[i+1]= tmp /10;
  23. }
  24. if(c[k]>0) k++;
  25. for(i = k; i >0; i--) printf("%d", c[i -1]);
  26. puts("");
  27. return0;
  28. }

 

 

posted on 2014-04-18 22:32  左手代码右手诗  阅读(164)  评论(0编辑  收藏  举报