大数相加
#include<iostream>#include<string>
#include<Cstring>
using namespace std;
int main()
{
int t;
cin>>t;
int num = 1;
while(t--)
{
char a[1000];
char b[1000];
char a1[1000];
char b1[1000];
char c[1000];
cin>>a>>b;
strcpy(a1,a);
strcpy(b1,b);
char *s1 = a;
char *s2 = b;
s1 = strrev(s1);
s2 = strrev(s2);
int i;
int m=0;
int len = strlen(s1)>=strlen(s2)?strlen(s1):strlen(s2);
for(i=0;i<len;i++)
{
if(*s1=='\0') *s1 = '0';
if(*s2=='\0') *s2 = '0';
cout<<*s1<<*s2<<endl;
int k = *s1-'0'+*s2-'0'+ m;
if(k > 9)
{
m = 1;
c[i] = k - 10 + '0';
}
else
{
m = 0;
c[i] = k + '0';
}
s1++;
s2++;
}
if(m == 1)
{
c[i] = m + '0';
c[i+1] = '\0';
}
else
{
c[i] = '\0';
}
cout<<"Case "<<num<<":"<<endl;
cout<<a1<<" + "<<b1<<" = "<<strrev(&c[0])<<endl;
memset(c,0,sizeof(c));
num++;
}
return 0;
}
杭电的1002题,大数相加会出现无法识别的某位