一道ACM题目 报错wrong answer
来源:学生作业帮 编辑:搜狗做题网作业帮 分类:综合作业 时间:2024/08/17 17:05:12
一道ACM题目 报错wrong answer
Children are taught to add multi-digit numbers from right-to-left one digit at a time.Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge.Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.
Each line of input contains two unsigned integers less than 10 digits.The last line of input contains 0 0.For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers,in the format shown below.
Sample Input123 456
555 555
123 594
0 0
Output for Sample Input No carry operation.
3 carry operations.
1 carry operation.
作答:
#include
using namespace std;
int main()
{
string s1,s2;
int n=0,N=0;
int b=1;
while(b=1)
{
cin>>s1>>s2;
int c1=s1.length()-1;
int c2=s2.length()-1;
if(s1[0]=='0'&&s2[0]=='0'&&c1==0&&c2==0)break;
else
while(c1>-1&&c2>-1)
{
if(s1[c1]+s2[c2]+N>105)
N=1,n+=1;
else N=0;
c1--,c2--;
}
if(c1>-1&&s2[c2]+N>9)n+=1;
if(c2>-1&&s1[c1]+N>9)n+=1;
if(n==0)cout
Children are taught to add multi-digit numbers from right-to-left one digit at a time.Many find the "carry" operation - in which a 1 is carried from one digit position to be added to the next - to be a significant challenge.Your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.
Each line of input contains two unsigned integers less than 10 digits.The last line of input contains 0 0.For each line of input except the last you should compute and print the number of carry operations that would result from adding the two numbers,in the format shown below.
Sample Input123 456
555 555
123 594
0 0
Output for Sample Input No carry operation.
3 carry operations.
1 carry operation.
作答:
#include
using namespace std;
int main()
{
string s1,s2;
int n=0,N=0;
int b=1;
while(b=1)
{
cin>>s1>>s2;
int c1=s1.length()-1;
int c2=s2.length()-1;
if(s1[0]=='0'&&s2[0]=='0'&&c1==0&&c2==0)break;
else
while(c1>-1&&c2>-1)
{
if(s1[c1]+s2[c2]+N>105)
N=1,n+=1;
else N=0;
c1--,c2--;
}
if(c1>-1&&s2[c2]+N>9)n+=1;
if(c2>-1&&s1[c1]+N>9)n+=1;
if(n==0)cout
![一道ACM题目 报错wrong answer](/uploads/image/z/165530-2-0.jpg?t=%E4%B8%80%E9%81%93ACM%E9%A2%98%E7%9B%AE+%E6%8A%A5%E9%94%99wrong+answer)
加上头文件#include
s1[c1]+s2[c2]是字符相加,不是数字相加.所以是字符5加字符5.字符5的ascll码是53,所以是s1+s2是106
再问: 还是不对
再答: 999 1应该是3 carry operations.
s1[c1]+s2[c2]是字符相加,不是数字相加.所以是字符5加字符5.字符5的ascll码是53,所以是s1+s2是106
再问: 还是不对
再答: 999 1应该是3 carry operations.