早教吧 育儿知识 作业答案 考试题库 百科 知识分享

一道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
▼优质解答
答案和解析
加上头文件#include
s1[c1]+s2[c2]是字符相加,不是数字相加.所以是字符5加字符5.字符5的ascll码是53,所以是s1+s2是106