杭电上的两道水题,自己找不出错在哪了。。。求助。1002 和1003.
1002#include<iostream>#include<string>usingnamespacestd;intmain(){stringstr1,str2;int...
1002
#include <iostream>
#include<string>
using namespace std;
int main()
{ string str1,str2;
int len1,len2,n;
int ary[10000];
cin>>n;
for(int k=1;k<=n;k++)
{
cin>>str1>>str2;
len1=str1.size();
len2=str2.size();
memset(ary,0,sizeof(ary));
cout<<"case "<<k<<":"<<endl;
cout<<str1<<" + "<<str2<<" = ";
int temp=0;
int i;
string t;
if(len1<len2)
{
temp=len1;len1=len2;len2=temp;
t=str1; str1=str2; str2=t;
}
for( i=0;i<(len1-len2);i++)
ary[i+1]=int(str1[i]-'0');
for( i=len1-len2;i<len1;i++)
ary[i+1]=int(str1[i]-'0'+str2[i-len1+len2]-'0');
for( i=len1;i>=1;i--)
{
if(ary[i]>=10)
{ ary[i]=ary[i]%10; ary[i-1]=ary[i-1]+1;}
}
for(int i=0;i<=len1;i++)
{ if(ary[0]==0){ary[0]=1; continue;}
cout<<ary[i];
}
cout<<endl;
cout<<endl;
}
//system("pause");
return 0;
}
1003
#include<iostream>
#include<cstdio>
using namespace std;
int a[100001];
int main()
{
int n,i,sum,time,max,temp;
scanf("%d",&time);
for(int m=1;m<=time;m++)
{
sum=0,max=-1000,temp=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
sum+=a[i];
if(sum>max)
{
temp=i;
max=sum;
}
}
cout<<"Case "<<m<<":"<<endl;
cout<<max<<" 1 "<<temp<<endl;
cout<<endl;
}
return 0;
}
难道是我理解错题了??、求解救啊,纠结很久了。。 展开
#include <iostream>
#include<string>
using namespace std;
int main()
{ string str1,str2;
int len1,len2,n;
int ary[10000];
cin>>n;
for(int k=1;k<=n;k++)
{
cin>>str1>>str2;
len1=str1.size();
len2=str2.size();
memset(ary,0,sizeof(ary));
cout<<"case "<<k<<":"<<endl;
cout<<str1<<" + "<<str2<<" = ";
int temp=0;
int i;
string t;
if(len1<len2)
{
temp=len1;len1=len2;len2=temp;
t=str1; str1=str2; str2=t;
}
for( i=0;i<(len1-len2);i++)
ary[i+1]=int(str1[i]-'0');
for( i=len1-len2;i<len1;i++)
ary[i+1]=int(str1[i]-'0'+str2[i-len1+len2]-'0');
for( i=len1;i>=1;i--)
{
if(ary[i]>=10)
{ ary[i]=ary[i]%10; ary[i-1]=ary[i-1]+1;}
}
for(int i=0;i<=len1;i++)
{ if(ary[0]==0){ary[0]=1; continue;}
cout<<ary[i];
}
cout<<endl;
cout<<endl;
}
//system("pause");
return 0;
}
1003
#include<iostream>
#include<cstdio>
using namespace std;
int a[100001];
int main()
{
int n,i,sum,time,max,temp;
scanf("%d",&time);
for(int m=1;m<=time;m++)
{
sum=0,max=-1000,temp=1;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
sum+=a[i];
if(sum>max)
{
temp=i;
max=sum;
}
}
cout<<"Case "<<m<<":"<<endl;
cout<<max<<" 1 "<<temp<<endl;
cout<<endl;
}
return 0;
}
难道是我理解错题了??、求解救啊,纠结很久了。。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询