ZOJ 2001题解字符串处理

 1 #include<stdio.h>
2 #include<string.h>
3
4 int main()
5 {
6 int i, ncases, len, c[102];
7 char a[100], b[100];
8
9 while(scanf("%d", &ncases) != EOF)
10 {
11
12 while(ncases--)
13 {
14 memset(c,0,sizeof(c));
15 scanf("%s %s",a, b);
16 if(a[0]=='0'&&b[0]=='0'&&strlen(a)==1&&strlen(b)==1)
17 {
18 printf("0\n");
19 continue;
20 } //这一步不可省略,否则易导致WA
21 int len1 = strlen(a);
22 int len2 = strlen(b);
23 for(i=0; i<len1 && i<len2; i++)
24 {
25 c[i] += a[i]-'0' + b[i]-'0';//c[i]后的+不可省略
26 if(c[i] > 9)
27 {
28 c[i] -= 10;
29 c[i+1]++;
30 }
31 }
32 while(i<len1 || i<len2)
33 {
34 if(i < len1)
35 c[i] += a[i]-'0';
36 if(i < len2)
37 c[i] += b[i]-'0';
38 if(c[i] > 9)
39 {
40 c[i] -= 10;
41 c[i+1]++;
42 }
43 i++;
44 }
45 while(i >= 0)
46 {
47 if(c[i] != 0)
48 {
49 len = i;
50 break;
51 }
52 i--;
53 }
54 i = 0;
55 while(!c[i])
56 i++;
57
58 while(i <= len)
59 {
60 printf("%d", c[i++]);
61 }
62 printf("\n");
63 }
64 }
65 return 0;
66 }
67

  

posted @ 2011-09-06 21:59  zhongya  阅读(203)  评论(0编辑  收藏  举报