题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
题目意思:就是大整数加法。
两年几前做的,纯粹是整理下来的。
1 #include <stdio.h> 2 #include <string.h> 3 4 #define max 1010 5 char a[max], b[max]; 6 7 int main() 8 { 9 int i, k, s, c, T, len1, len2; 10 scanf("%d", &T); 11 for (k = 0; k < T; k++) 12 { 13 int A[max] = {0}, B[max] = {0}; 14 scanf("%s %s", &a, &b); 15 len1 = strlen(a); 16 len2 = strlen(b); 17 for (i = 0; i < len1; i++) 18 A[i] = a[len1-i-1] - ‘0‘; 19 for (i = 0; i < len2; i++) 20 B[i] = b[len2-i-1] - ‘0‘; 21 c = 0; 22 for (i = 0; i < max; i++) 23 { 24 s = A[i] + B[i] + c; 25 A[i] = s % 10; 26 c = s / 10; 27 } 28 printf("Case %d:\n%s + %s = ", k+1, a, b); 29 for (i = max-1; i >= 0; i--) 30 { 31 if (A[i]) 32 break; 33 } 34 for ( ; i >= 0; i--) 35 printf("%d", A[i]); 36 printf("\n"); 37 if (k < T - 1) 38 printf("\n"); 39 } 40 return 0; 41 }
时间: 2024-11-25 07:49:56