标签:
1 #include<stdio.h> 2 #include<string.h> 3 #define max(a, b) a > b ? a:b 4 5 char a[1024]; 6 char b[1024]; 7 char c[1024]; 8 int i; 9 10 void reverse(char *a) 11 { 12 int aa = strlen(a); 13 char t; 14 for(i=0; i<aa/2; i++) 15 { 16 t = a[i]; 17 a[i] = a[aa-1-i]; 18 a[aa-1-i] = t; 19 } 20 } 21 void add(char *a, char *b, char *c) 22 { 23 int cc = 0, aa = strlen(a), bb = strlen(b); 24 int len = max(aa, bb); 25 for(i=0; i<aa; i++) 26 { 27 a[i] = a[i]-‘0‘; 28 } 29 for(i=0; i<bb; i++) 30 { 31 b[i] = b[i]-‘0‘; 32 } 33 for(i=0; i<len; i++) 34 { 35 c[i] = (a[i]+b[i]+cc) % 10 + ‘0‘; 36 cc = (a[i]+b[i]+cc) / 10; 37 } 38 if(cc) c[i++] = cc + ‘0‘; 39 c[i] = ‘\0‘; 40 } 41 void print(char *c) 42 { 43 for(i = strlen(c)-1; i>=0; i--) 44 printf("%c", c[i]); 45 printf("\n"); 46 } 47 main() 48 { 49 int n, j; 50 scanf("%d", &n); 51 for(j=1; j<=n; j++) 52 { 53 memset(a, 0, sizeof(a)); 54 memset(b, 0, sizeof(b)); 55 memset(c, 0, sizeof(c)); 56 scanf("%s %s", a, b); 57 printf("Case %d:\n", j); 58 printf("%s + %s = ", a, b); 59 reverse(a), reverse(b); 60 add(a, b, c); 61 print(c); 62 if(j != n) printf("\n"); 63 } 64 65 }
标签:
原文地址:http://www.cnblogs.com/forerve/p/4339092.html