浙大2006考研复试上机题——火星A+B

题目链接:http://ac.jobdu.com/problem.php?id=1016

今天做了下这道五星的题,题目倒是不难,但是由于疏忽导致一直RE,最后玉玉发现了问题所在,才使得悲剧没有继续。

这题的思路类似于做高精度的运算,就是按位加,如果超过相应位的权值,就向上进位。

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 using namespace std;
5
6 char stra[1000],strb[1000];
7 int a[30],b[30],c[30];
8 int w[26] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
9
10 int main()
11 {
12 int i,j,k,t;
13 while(scanf("%s %s",stra,strb) != EOF)
14 {
15 if(strcmp(stra,"0") == 0 || strcmp(strb,"0") == 0)
16 break;
17 int lensa = strlen(stra);
18 int lensb = strlen(strb);
19 memset(a,0,sizeof(a));
20 memset(b,0,sizeof(b));
21 memset(c,0,sizeof(c));
22 for(i=0,j=0;i<lensa;i++)
23 {
24 if(stra[i] != ',')
25 {
26 if(i < lensa-1 && stra[i+1] != ',')
27 {
28 a[j] = 10 * (stra[i]-'0') + stra[i+1]-'0';
29 i++;
30 }
31 else
32 a[j] = stra[i] - '0';
33 j++;
34 }
35 }
36 int lena = j;
37 for(i=0,j=0;i<lensb;i++)
38 {
39 if(strb[i] != ',')
40 {
41 if(i < lensb-1 && strb[i+1] != ',')
42 {
43 b[j] = 10 * (strb[i]-'0') + strb[i+1]-'0';
44 i++;
45 }
46 else
47 b[j] = strb[i] - '0';
48 j++;
49 }
50 }
51 int lenb = j;
52 for(i=lena-1,j=lenb-1,k=0,t=0;i>=0&&j>=0;i--,j--,k++,t++)
53 {
54 c[t] += a[i] + b[j];
55 if(c[t] >= w[k])
56 {
57 c[t+1] += c[t] / w[k];
58 c[t] %= w[k];
59 }
60 }
61 while(i >= 0)
62 {
63 c[t] += a[i];
64 if(c[t] >= w[k])
65 {
66 c[t+1] += c[t] / w[k];
67 c[t] %= w[k];
68 }
69 t++;
70 k++;
71 i--;
72 }
73 while(j >= 0)
74 {
75 c[t] += b[j];
76 if(c[t] >= w[k])
77 {
78 c[t+1] += c[t] / w[k];
79 c[t] %= w[k];
80 }
81 t++;
82 k++;
83 j--;
84 }
85 while(!c[t]) t--;
86 for(;t>0;t--)
87 printf("%d,",c[t]);
88 printf("%d\n",c[0]);
89 }
90 return 0;
91 }


 

posted @ 2012-03-06 22:11  HanTQ  阅读(150)  评论(0编辑  收藏  举报