杭电1230 求大牛指出错误,一直wa...在线等。。。
#include<iostream>usingnamespacestd;intmain(){voidadds(inta[],intb[],intx,inty);inta[...
#include<iostream>using namespace std;
int main()
{
void adds(int a[],int b[],int x,int y);
int a[30],b[30],i,lena,lenb;
while(cin>>a[0])
{
lena=1;
while(getchar()==',') cin>>a[lena++];
cin>>b[0];
if(a[0]==0||b[0]==0) break;
lenb=1;
while(getchar()==',') cin>>b[lenb++];
if(lenb>lena) adds(b,a,lenb,lena);
else adds(a,b,lena,lenb);
}
return 0;
}
void adds(int a[],int b[],int x,int y){
int i,j,k;
int d[25]={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};
k=0;
for(i=1;i<=y;i++)
{
a[x-i]+=b[y-i]+k;
if(a[x-i]>=d[i-1])
{
k=1;
a[x-i]-=d[i-1];
}
else k=0;
}
j=i;
while(k>0&&j<=x)
{
if(a[x-j]==d[j-1]-1)
{
k=1;
a[x-j]=0;
}
else k=0;
j++;
}
if(k>0) cout<<"1,";
for(i=0;i<x-1;i++) cout<<a[i]<<',';
cout<<a[x-1]<<endl;
} 展开
int main()
{
void adds(int a[],int b[],int x,int y);
int a[30],b[30],i,lena,lenb;
while(cin>>a[0])
{
lena=1;
while(getchar()==',') cin>>a[lena++];
cin>>b[0];
if(a[0]==0||b[0]==0) break;
lenb=1;
while(getchar()==',') cin>>b[lenb++];
if(lenb>lena) adds(b,a,lenb,lena);
else adds(a,b,lena,lenb);
}
return 0;
}
void adds(int a[],int b[],int x,int y){
int i,j,k;
int d[25]={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};
k=0;
for(i=1;i<=y;i++)
{
a[x-i]+=b[y-i]+k;
if(a[x-i]>=d[i-1])
{
k=1;
a[x-i]-=d[i-1];
}
else k=0;
}
j=i;
while(k>0&&j<=x)
{
if(a[x-j]==d[j-1]-1)
{
k=1;
a[x-j]=0;
}
else k=0;
j++;
}
if(k>0) cout<<"1,";
for(i=0;i<x-1;i++) cout<<a[i]<<',';
cout<<a[x-1]<<endl;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询