永发信息网

杭电1002

答案:2  悬赏:70  手机版
解决时间 2021-05-31 20:34
  • 提问者网友:酱爆肉
  • 2021-05-31 17:31

#include<iostream>
using namespace std;
int main()
{
char a[1100],b[1100];
int a1[1100],b1[1100],c[1100]={0},n,n1;
cin>>n;
for(n1=0;n1<n;n1++)
{
cin>>a>>b;
int x,y,i,t=0,p,q,o;
x=strlen(a);
y=strlen(b);
for(i=x-1,t=0;i>=0;i--,t++)
{
a1[t]=a[i]-'0';
}
for(i=y-1,t=0;i>=0;i--,t++)
{
b1[t]=b[i]-'0';
}
q=x>y?x:y;
p=x<y?x:y;
for(i=0;i<p;i++)
{
c[i]=(a1[i]+b1[i])%10+c[i];
if(a1[i]+b1[i]>=10)
{
c[i+1]++;
}
}
if(x>y)
{
for(i=y;i<x;i++)
{

c[i]=a1[i]+c[i];
if(c[i]>=10)
{
c[i]=c[i]%10;
c[i+1]++;
}
}
}
else if(y>x)
{
for(i=x;i<y;i++)
{
c[i]=b1[i]+c[i];
if(c[i]>=10)
{
c[i]=c[i]%10;
c[i+1]++;
}
}
}
cout<<"Case "<<n1+1<<":"<<endl;
for(i=0;i<x;i++)
{
cout<<a[i];
}
cout<<" + ";
for(i=0;i<y;i++)
{
cout<<b[i];
}
cout<<" = ";
if(c[q]==1)
{
cout<<c[q];
}
for(i=q-1;i>0;i--)
{
cout<<c[i];
}
cout<<c[0]<<endl;
for(i=0;i<1100;i++)
{
c[i]=0;
a1[i]=0;
b1[i]=0;
}
if(n1<n-1)
{
cout<<endl;
}

}

return 0;
}

我哪里错????????

最佳答案
  • 五星知识达人网友:玩世
  • 2021-05-31 17:59

杭电的ACM online吧。我也是杭电的,你可以参考下我的:


#include<string>
#include<iostream>
using namespace std;


int main()
{
int T,i,j,v,l1,l2,l3,k,m,n,p,le;char str1[1000],str2[1000],str3[1000];
cin>>T;
for(i=0;i<T;i++)
{
cin>>str1>>str2;
l1=strlen(str1);l2=strlen(str2);
l3=(l1>l2)?l1:l2;str3[l3+1]='\0';for(v=0;v<=l3;v++)str3[v]='0';

for(k=0;l3-k>0;k++)
{
if(l1-k-1>=0&&l2-k-1>=0)
{
m=str1[l1-k-1]-'0';n=str2[l2-k-1]-'0';p=m+n;
if(p>9)
{
str3[l3-k]=str3[l3-k]+p-10;str3[l3-k-1]=str3[l3-k-1]+1;le=0;
while(str3[l3-k-1-le]>=58)
{
str3[l3-k-2-le]++;
str3[l3-k-1-le]=str3[l3-k-1-le]-10;
le++;
}
}
else
{
str3[l3-k]=str3[l3-k]+p;le=0;
while(str3[l3-k-le]>=58)
{
str3[l3-k-1-le]++;
str3[l3-k-le]=str3[l3-k-le]-10;
le++;
}
}
}
if(l1-k-1<0&&l2-k-1>=0)
{
n=str2[l2-k-1]-'0';p=n;str3[l3-k]=str3[l3-k]+p;le=0;
while(str3[l3-k-le]>=58)
{
str3[l3-k-1-le]=str3[l3-k-1-le]+1;
str3[l3-k-le]=str3[l3-k-le]-10;
le++;
}
}
if(l2-k-1<0&&l1-k-1>=0)
{
n=str1[l1-k-1]-'0';p=n;str3[l3-k]=str3[l3-k]+p;le=0;
while(str3[l3-k-le]>=58)
{
str3[l3-k-1-le]++;
str3[l3-k-le]=str3[l3-k-le]-10;
le++;
}
}
}


if(str3[0]-'0'==0)for(j=0;j<=l3;j++)str3[j]=str3[j+1];
cout<<"Case "<<i+1<<":"<<endl;
cout<<str1<<" + "<<str2<<" = "<<str3<<endl;
if(i!=T-1)cout<<endl;


}
return 0;
}


全部回答
  • 1楼网友:长青诗
  • 2021-05-31 18:27
#include<stdio.h> #include<string.h> int main(){ char x[1001],y[1001],z[1001]; int n,i,j,k,m,o; scanf("%d",&n); o=n; while(n--) { scanf("%s%s",x,y); i=strlen(x); j=strlen(y); for(k=0,m=0;i>0&&j>0;i--,j--) { m+=x[i-1]-'0'+y[j-1]-'0'; z[k++]=m%10+'0'; m/=10; } for(;i>0;i--) { m+=x[i-1]-'0'; z[k++]=m%10+'0'; m/=10; } for(;j>0;j--) { m+=y[j-1]-'0'; z[k++]=m%10+'0'; m/=10; } if(m>0) z[k++]=m%10+'0'; printf("Case %d:\n%s + %s = ",o-n,x,y); for(;k>0;k--) printf("%c",z[k-1]); printf("\n"); if(n) printf("\n"); } return 0;}貌似 更加帅气!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯