zoj Martian Addition

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1205

同样是大整数加法的练手题目。题目意思就是:二十进制的大数相加,注意进位,注意字母和数字间的转换和都是零时有输出就可以了

 1 #include<string.h>
2 #include<iostream>
3 #include<stdio.h>
4 using namespace std;
5 #define N 110
6 char str[N],sbr[N],t[N];
7 int a[N],b[N],c[N];
8 int main()
9 {
10 int i,j,k,len1,len2,len;
11 while(cin>>str>>sbr)
12 {
13 memset(c,0,sizeof(c));
14 memset(a,0,sizeof(a));
15 memset(b,0,sizeof(b));
16 len1=strlen(str);
17 len2=strlen(sbr);
18 len=len1>len2?len1:len2;
19 i=0;
20 for(j=len1-1;j>=0;j--)
21 {
22 if(str[j]<='9'&&str[j]>='0')
23 a[i++]+=str[j]-'0';
24 else
25 a[i++]+=str[j]-87;
26 }
27 i=0;
28 for(j=len2-1;j>=0;j--)
29 {
30 if(sbr[j]<='9'&&sbr[j]>='0')
31 b[i++]+=sbr[j]-'0';
32 else
33 b[i++]+=sbr[j]-87;
34 }
35 for(i=0;i<len;i++)
36 {
37 c[i]+=(a[i]+b[i]);
38 if(c[i]>19)
39 {
40 c[i+1]=c[i]/20; //注意这里进位时与乘法进位相同
41 c[i]%=20;
42 }
43 }
44 i=len;
45 int flag=1;
46 while(!c[i])
47 {
48 i--;
49 if(i==-1)
50 {
51 cout<<"0"<<endl; //零的输出
52 flag=0;
53 break;
54 }
55 }
56 if(!flag) continue;
57 k=0;
58 for(j=i;j>=0;j--)
59 {
60 if(c[j]>=0&&c[j]<=9)
61 t[k++]=(c[j]+'0');
62 else
63 t[k++]=(c[j]+87);
64 }
65 for(j=0;j<k;j++)
66 cout<<t[j];
67 cout<<endl;
68
69 }
70 return 0;
71 }


原文地址:https://www.cnblogs.com/fxh19911107/p/2274582.html