Hdoj 1002
问题描述
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
输入描述
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 two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
输出描述
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
思路
简单的大数问题
代码
1 #include<stdio.h> 2 #include<string.h> 3 4 int main() 5 { 6 int n, j; 7 scanf("%d", &n); 8 for(j = 1; j <= n; j++) 9 { 10 char ch1[1001], ch2[1001]; 11 scanf("%s", ch1); 12 int s1 = strlen(ch1), i; 13 int a[1001] = {0}, b[1001] = {0}; 14 for(i = s1-1; i >= 0; i--) { a[s1-i] = ch1[i] - '0'; } 15 scanf("%s", ch2); 16 int s2 = strlen(ch2); 17 for(i = s2-1; i >= 0; i--) { b[s2-i] = ch2[i] - '0'; } 18 printf("Case %d:\n%s + %s = ", j, ch1, ch2); 19 int s = (s1>s2?s1:s2), temp=0; 20 for(i = 1 ; i <= s; i++) 21 { 22 a[i] += b[i] + temp; 23 temp = a[i] / 10; 24 a[i] %= 10; 25 } 26 if(temp != 0) printf("%d",temp); 27 for(i=s;i>0;i--) printf("%d",a[i]); 28 if(j < n) printf("\n"); 29 printf("\n"); 30 } 31 }