A+b (Big number Version) time limit:2000/1000ms (java/others) problem Description:
Given integers A and B, your job is to calculate the Sum of A + B.
Input:
The first line of the input contains an integer T (1≤t≤20) which means the number of test cases. Then T lines follow, each line consists of both positive integers, A and B. Notice that the integers is very large, that M EANs should not process them by using 32-bit integer. Assume the length of each integer would not exceed 400.
Output:
For the test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line is the equation "A + b = sum", sum means the result of A + B. Note there is some spaces int the Equati On. Output a blank line between the test cases.
Sample Input:
31 2112233445566778899 99887766554433221133333333333333333333333333 100000000000000000000
Sample Output:
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110Case 3:3.,333,333,333,333,33e,+25 + 10 0000000000000000000 = 33333433333333333333333333
This problem is of moderate difficulty.
1 Core code:2scanf"%s%s", S1,S2);3Len1=strlen (S1); Len2=strlen (S2); 4 for(i=len1-1, j=0; i>=0; i--)//converts an array of S1 strings to a numeric array, in reverse order5num1[j++]=s1[i]-'0';6 for(i=len2-1, j=0; i>=0; i--) 7num2[j++]=s2[i]-'0'; 8 for(i=0; i<n;i++) 9 { Tennum1[i]+=Num2[i]; One if(num1[i]>9)//Rounding A { -num1[i]-=Ten;//the largest addition is 9+9=18 -num1[i+1]++;//so add 1 . the } - } -printf"Case %d:\n%s +%s =", X,S1,S2); -X + +; + for(i=n-1;(i>=0) && (num1[i]==0); i--);//empty statement minus the extra 0 - for(; i>=0; i--) +printf ("%d", num1[i]);//Reverse Output Results A
An additional AC code is attached:
1#include <stdio.h>2#include <string.h>3 intMain ()4 {5 Chara[ +],b[ +],c[1001];6 inti,j=1, p=0, n,n1,n2;7scanf"%d",&n);8 while(n)9 {Tenscanf"%s%s", A, b); Oneprintf"Case %d:\n", j); Aprintf"%s +%s =", A, b); -N1=strlen (a)-1; -N2=strlen (b)-1; the for(i=0; n1>=0|| n2>=0; I++,n1--, n2--) - { - if(n1>=0&&n2>=0) {c[i]=a[n1]+b[n2]-'0'+p;} - if(n1>=0&&n2<0) {c[i]=a[n1]+p;} + if(n1<0&&n2>=0) {c[i]=b[n2]+p;} -p=0; + if(c[i]>'9') {c[i]=c[i]-Ten;p =1;} A } at if(p==1) {printf ("%d", p);} - while(i--) -{printf ("%c", C[i]);} -J + +; - if(n!=1) {printf ("\ n");} - Else{printf ("\ n");} inn--; - } to}
Gdufe ACM-1002