杭电acm 1002,求大神指导!http://acm.hdu.edu.cn/showproblem.php?pid=1002
#include"string.h"#include<stdlib.h>#include<iostream.h>intmain(){intn;cin>>n;intcoun...
#include "string.h"
#include<stdlib.h>
#include<iostream.h>
int main()
{
int n;
cin>>n;
int count=n;
while (n>0)
{
char a[1001],b[1001],c[1002];
cin>>a;
cin>>b;
int len;
int i = strlen(a);
int j = strlen(b);
int temp=0;
if (i>=j)//判断哪个长
{len=i;
while(j>0&&i>0)
{
int s=0;
s=a[i-1]+b[j-1]+temp;
s=s-96;
temp = s/10;
c[i]=s%10;
i--;
j--;
}
while (i>0)
{
int s=0;
s=a[i-1]+temp;
s = s-48;
temp = s/10;
c[i]=s%10;
i--;
}
c[0]=temp;
}
else
{len=j;
while(j>0&&i>0)
{
int s=0;
s=a[i-1]+b[j-1]+temp;
s=s-96;
temp = s/10;
c[j]=s%10;
i--;
j--;
}
while (j>0)
{
int s=0;
s=b[j-1]+temp;
s=s-48;
temp = s/10;
c[j]=s%10;
j--;
}
c[0]=temp;
}
cout<<"case"<<count -n+1<<":"<<endl<<a <<" + "<<b<<" = ";
if(c[0]==1)
cout<<1;
for(int k=1;k<=len;k++)
cout<<c[k]+0;
cout<<endl;
cout<<endl;
n--;
}
return 0;
} 展开
#include<stdlib.h>
#include<iostream.h>
int main()
{
int n;
cin>>n;
int count=n;
while (n>0)
{
char a[1001],b[1001],c[1002];
cin>>a;
cin>>b;
int len;
int i = strlen(a);
int j = strlen(b);
int temp=0;
if (i>=j)//判断哪个长
{len=i;
while(j>0&&i>0)
{
int s=0;
s=a[i-1]+b[j-1]+temp;
s=s-96;
temp = s/10;
c[i]=s%10;
i--;
j--;
}
while (i>0)
{
int s=0;
s=a[i-1]+temp;
s = s-48;
temp = s/10;
c[i]=s%10;
i--;
}
c[0]=temp;
}
else
{len=j;
while(j>0&&i>0)
{
int s=0;
s=a[i-1]+b[j-1]+temp;
s=s-96;
temp = s/10;
c[j]=s%10;
i--;
j--;
}
while (j>0)
{
int s=0;
s=b[j-1]+temp;
s=s-48;
temp = s/10;
c[j]=s%10;
j--;
}
c[0]=temp;
}
cout<<"case"<<count -n+1<<":"<<endl<<a <<" + "<<b<<" = ";
if(c[0]==1)
cout<<1;
for(int k=1;k<=len;k++)
cout<<c[k]+0;
cout<<endl;
cout<<endl;
n--;
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询