早教吧作业答案频道 -->其他-->
一道ACM题目报错wronganswerChildrenaretaughttoaddmulti-digitnumbersfromright-to-leftonedigitatatime.Manyfindthe"carry"operation-inwhicha1iscarriedfromonedigitpositiontobeaddedtothenext-tobeasignificantch
题目详情
一道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
▼优质解答
答案和解析
加上头文件#include
s1[c1]+s2[c2]是字符相加,不是数字相加.所以是字符5加字符5.字符5的ascll码是53,所以是s1+s2是106
s1[c1]+s2[c2]是字符相加,不是数字相加.所以是字符5加字符5.字符5的ascll码是53,所以是s1+s2是106
看了 一道ACM题目报错wrong...的网友还看了以下:
已知正方形ABCD-A1B1C1D1中,点E,F,G,H,M,N分别是AB,BC,CC1,AA1, 2020-05-16 …
在正方形ABCD-A1B1C1D1中,E,F,G,H,M,N分别是正方形的棱A1A,AB,BC,C 2020-05-16 …
一道高二的基本的几何题目在正方体ABCD-A1B1C1D1中,E,F,G,H,M,N分别是正方形的 2020-06-03 …
纵横字谜之英语暑假作业题(s)(o)(r)(r)(n)(g)(r)(e))(a)(k)(k)(e) 2020-06-06 …
在△ABC中,BC>AB,BD平分∠ABC交AC于D,如图,CP垂直BD,垂足为P,AQ垂直BP, 2020-06-15 …
1.e为方阵A的特征值,则矩阵kA,A的平方,aA+bE,A的m次方,A的逆,A的伴随阵分别有特征 2020-06-18 …
求证E,F,G,M,N,H六点共面已知正方体ABCD-A1B1C1D1中,点E,F,G,H,M,N 2020-07-09 …
在正方体ABCD-A1B1C1D1中,E,F,G,H,M,N分别为正方体相应棱长的中点,求证E,F 2020-07-09 …
正方体中,E,F,G,H,M,N分别是正方体的棱A1A,AB,BC,CC1,C1D1,D1A1的中 2020-07-09 …
下表中,A.B.C,D,E,F,G,H.M各代表一个互不相干的非零得数字,其中A+B=14,M/C= 2020-11-16 …