杭电http://acm.hdu.edu.cn/showproblem.php?pid=1002测试是对的啊,求找错
#include<iostream>#include<string>#include<fstream>#include<sstream>usingnamespacestd...
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int f=0;int times=1;
while(n--)
{
string s1,s2,s,mins,maxs,ts;
int i;
int max,min;
cin>>s1>>s2;
min=(s1.length()<=s2.length())?s1.length():s2.length();
max=(s1.length()>=s2.length())?s1.length():s2.length();
if(s1.length()!=s2.length())
{
mins=(s1.length()<s2.length())?s1:s2;
maxs=(s1.length()>s2.length())?s1:s2;
}
else
{maxs=s1;mins=s2;}
for(i=1;i<=max-min;i++)
{
mins='0'+mins;
}
if(f++) cout<<endl;
if(max==1&&min==1)
{ cout<<"Case "<<times<<":"<<endl;
cout<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<endl;
times++;
continue;
}
int flag=0;
ts=(char)((maxs[max-1]+mins[max-1]-96)%10+48)+ts;
if((maxs[max-1]+mins[max-1]-96)>=10) flag=1;
for(i=max-2;i>=1;i--)
{
if(flag)
ts=(char)((maxs[i]+mins[i]-95)%10+48)+ts;
else
ts=(char)((maxs[i]+mins[i]-96)%10+48)+ts;
if((maxs[i]+mins[i])-96>=10) flag=1;
}
if(flag)
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-95<<ts<<endl;
else
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<ts<<endl;
times++;
}
}
return 0;
} 展开
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int f=0;int times=1;
while(n--)
{
string s1,s2,s,mins,maxs,ts;
int i;
int max,min;
cin>>s1>>s2;
min=(s1.length()<=s2.length())?s1.length():s2.length();
max=(s1.length()>=s2.length())?s1.length():s2.length();
if(s1.length()!=s2.length())
{
mins=(s1.length()<s2.length())?s1:s2;
maxs=(s1.length()>s2.length())?s1:s2;
}
else
{maxs=s1;mins=s2;}
for(i=1;i<=max-min;i++)
{
mins='0'+mins;
}
if(f++) cout<<endl;
if(max==1&&min==1)
{ cout<<"Case "<<times<<":"<<endl;
cout<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<endl;
times++;
continue;
}
int flag=0;
ts=(char)((maxs[max-1]+mins[max-1]-96)%10+48)+ts;
if((maxs[max-1]+mins[max-1]-96)>=10) flag=1;
for(i=max-2;i>=1;i--)
{
if(flag)
ts=(char)((maxs[i]+mins[i]-95)%10+48)+ts;
else
ts=(char)((maxs[i]+mins[i]-96)%10+48)+ts;
if((maxs[i]+mins[i])-96>=10) flag=1;
}
if(flag)
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-95<<ts<<endl;
else
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<ts<<endl;
times++;
}
}
return 0;
} 展开
3个回答
展开全部
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
int n;
cin>>n; //为符合题意,n应单独输入,不应放入while循环中
int f=0;int times=1;
while(n--)
{
string s1,s2,s,mins,maxs,ts;
int i;
int max,min;
cin>>s1>>s2;
min=(s1.length()<=s2.length())?s1.length():s2.length();
max=(s1.length()>=s2.length())?s1.length():s2.length();
if(s1.length()!=s2.length())
{
mins=(s1.length()<s2.length())?s1:s2;
maxs=(s1.length()>s2.length())?s1:s2;
}
else
{maxs=s1;mins=s2;}
for(i=1;i<=max-min;i++)
{
mins='0'+mins;
}
if(f++) cout<<endl;
if(max==1&&min==1)
{ cout<<"Case "<<times<<":"<<endl;
cout<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<endl;
times++;
continue;
}
int flag=0;
ts=(char)((maxs[max-1]+mins[max-1]-96)%10+48)+ts;
if((maxs[max-1]+mins[max-1]-96)>=10) flag=1;
for(i=max-2;i>=1;i--) //此处循环体,每次相加完成都应判断是否有进位
{ //原来的语句中没有写如何flag=0;即一旦flag=1,则始终flag=1
if(flag) //取决于是否有进位,flag置0置1的方式有区别
{
ts=(char)((maxs[i]+mins[i]-95)%10+48)+ts;
if((maxs[i]+mins[i])-95>=10) flag=1; //有进位,>=9就应再进位
else flag=0;
}
else
{
ts=(char)((maxs[i]+mins[i]-96)%10+48)+ts;
if((maxs[i]+mins[i])-96>=10) flag=1; //无进位,>=10才应再进位
else flag=0;
}
}
if(flag)
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-95<<ts<<endl;
else
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<ts<<endl;
times++;
}
return 0;
}
错误已经在注释中给出.修改后的代码在杭电OJ提交AC通过了.局部的flag标志错误.
给出测试数据:1238 267 (正确结果应该是1505,程序的实际结果是2505)
同时按照题目的输入输出要求,进行了修改.即输入一次n,执行n次循环后应结束程序.
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
int main()
{
int n;
cin>>n; //为符合题意,n应单独输入,不应放入while循环中
int f=0;int times=1;
while(n--)
{
string s1,s2,s,mins,maxs,ts;
int i;
int max,min;
cin>>s1>>s2;
min=(s1.length()<=s2.length())?s1.length():s2.length();
max=(s1.length()>=s2.length())?s1.length():s2.length();
if(s1.length()!=s2.length())
{
mins=(s1.length()<s2.length())?s1:s2;
maxs=(s1.length()>s2.length())?s1:s2;
}
else
{maxs=s1;mins=s2;}
for(i=1;i<=max-min;i++)
{
mins='0'+mins;
}
if(f++) cout<<endl;
if(max==1&&min==1)
{ cout<<"Case "<<times<<":"<<endl;
cout<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<endl;
times++;
continue;
}
int flag=0;
ts=(char)((maxs[max-1]+mins[max-1]-96)%10+48)+ts;
if((maxs[max-1]+mins[max-1]-96)>=10) flag=1;
for(i=max-2;i>=1;i--) //此处循环体,每次相加完成都应判断是否有进位
{ //原来的语句中没有写如何flag=0;即一旦flag=1,则始终flag=1
if(flag) //取决于是否有进位,flag置0置1的方式有区别
{
ts=(char)((maxs[i]+mins[i]-95)%10+48)+ts;
if((maxs[i]+mins[i])-95>=10) flag=1; //有进位,>=9就应再进位
else flag=0;
}
else
{
ts=(char)((maxs[i]+mins[i]-96)%10+48)+ts;
if((maxs[i]+mins[i])-96>=10) flag=1; //无进位,>=10才应再进位
else flag=0;
}
}
if(flag)
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-95<<ts<<endl;
else
cout<<"Case "<<times<<":"<<endl<<s1<<" + "<<s2<<" = "<<maxs[0]+mins[0]-96<<ts<<endl;
times++;
}
return 0;
}
错误已经在注释中给出.修改后的代码在杭电OJ提交AC通过了.局部的flag标志错误.
给出测试数据:1238 267 (正确结果应该是1505,程序的实际结果是2505)
同时按照题目的输入输出要求,进行了修改.即输入一次n,执行n次循环后应结束程序.
展开全部
大数相加,判断ascll码的界值是否超出范围,超出向上加一位,不超则原地相加
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这输出有问题啊,像题目给的样例,是两个测试用例,两个计算完成以后,你的程序并没有结束。而且两个用例之间要隔一行。
应该是:
2
1 2
Case 1:
1 + 2 = 3
1 2
Case 2:
1 + 2 = 3
但是你的是:
2
1 2
Case 1:
1 + 2 = 3
1 2
Case 2:
1 + 2 = 3
这个换行好像换错地方了把
应该是:
2
1 2
Case 1:
1 + 2 = 3
1 2
Case 2:
1 + 2 = 3
但是你的是:
2
1 2
Case 1:
1 + 2 = 3
1 2
Case 2:
1 + 2 = 3
这个换行好像换错地方了把
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询