【HDOJ】1230 火星A+B

个人觉得这道题没那么水,wa了几次,才发现自己居然没有给srcb数组reset,打错了。搞死啊。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define MAXNUM 26
 5 int srca[MAXNUM];
 6 int srcb[MAXNUM];
 7 int isPrime[MAXNUM] = {2, 3, 5, 7, 11,13,17,19,23,29,31,37,41,
 8                        43,47,53,59,61,67,71,73,79,83,89,97,101};
 9 
10 
11 void reverse(int a[], int beg, int end) {
12     int tmp, i, k=beg+end-1;
13 
14     for (i=beg; i+i<=k; ++i) {
15         tmp = a[i];
16         a[i] = a[k-i];
17         a[k-i] = tmp;
18     }
19 }
20 
21 void testsrc(int src[], int len) {
22     int i;
23     for (i=0;i<len;++i)
24         printf("%d ", src[i]);
25     printf("\n");
26 }
27 
28 void magicadd(int lena, int lenb) {
29     int ov = 0;
30     int i;
31 
32     reverse(srca, 0, lena);
33     reverse(srcb, 0, lenb);
34 
35     for (i=0; i<MAXNUM; ++i) {
36         srca[i] += (srcb[i] + ov);
37         ov = srca[i] / isPrime[i];
38         srca[i] = srca[i] % isPrime[i];
39     }
40 }
41 
42 int main() {
43     int lena, lenb, flg, tmp;
44     char ch;
45     int i;
46 
47     while (1) {
48         memset(srca, 0, sizeof(srca));
49         memset(srcb, 0, sizeof(srcb));
50         flg = lena = lenb = tmp = 0;
51         while (1) {
52             ch = getchar();
53             if (ch == ' ') {
54                 srca[lena++] = tmp;
55                 flg = 1;
56                 tmp = 0;
57             } else if (ch == ',') {
58                 if (flg)
59                     srcb[lenb++] = tmp;
60                 else
61                     srca[lena++] = tmp;
62                 tmp = 0;
63             } else if (ch == '\n') {
64                 srcb[lenb++] = tmp;
65                 break;
66             } else {
67                 tmp = tmp*10+ch-'0';
68             }
69         }
70         if (lena==1 && lenb==1 && srca[0]==0 && srcb[0]==0)
71             break;
72         magicadd(lena, lenb);
73         i = MAXNUM-1;
74         while (srca[i] == 0)
75             i--;
76         for (; i>0; i--)
77             printf("%d,", srca[i]);
78         printf("%d\n", srca[0]);
79     }
80 
81     return 0;
82 }

 

posted on 2014-03-21 16:30  Bombe  阅读(281)  评论(0编辑  收藏  举报

导航