一道acm题目 报错是wrong answer 求大神找错
题目:Childrenaretaughttoaddmulti-digitnumbersfromright-to-leftonedigitatatime.Manyfindt...
题目:
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 Input
123 456
555 555
123 594
0 0
Output for Sample Input
No carry operation.
3 carry operations.
1 carry operation.
作答
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x,y;
while(getline(cin,x,' '),getline(cin,y)&&x!="0"||y!="0")
{
int n=x.length();
int m=y.length();
int max=(n>=m)?n:m;
int min=(n<m)?n:m;
string maxs=(n>=m)?x:y;
string mins=(n<m)?x:y;
int count=0;
int i=max;
for(i;i>0;i--)
{
if(maxs[max-1]+mins[min-1]>=106||maxs[max-1]>=58)
{
count++;
maxs[max-2]++;
max--;
min--;
}
else
{
max--;
min--;
}
}
if(count>1)
{
cout<<count<<" "<<"carry "<<"operations"<<endl;
}
else if(count==1)
{
cout<<"1 carry operation"<<endl;
}
else
{
cout<<"No carry operation"<<endl;
}
}
return 0;
} 展开
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 Input
123 456
555 555
123 594
0 0
Output for Sample Input
No carry operation.
3 carry operations.
1 carry operation.
作答
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x,y;
while(getline(cin,x,' '),getline(cin,y)&&x!="0"||y!="0")
{
int n=x.length();
int m=y.length();
int max=(n>=m)?n:m;
int min=(n<m)?n:m;
string maxs=(n>=m)?x:y;
string mins=(n<m)?x:y;
int count=0;
int i=max;
for(i;i>0;i--)
{
if(maxs[max-1]+mins[min-1]>=106||maxs[max-1]>=58)
{
count++;
maxs[max-2]++;
max--;
min--;
}
else
{
max--;
min--;
}
}
if(count>1)
{
cout<<count<<" "<<"carry "<<"operations"<<endl;
}
else if(count==1)
{
cout<<"1 carry operation"<<endl;
}
else
{
cout<<"No carry operation"<<endl;
}
}
return 0;
} 展开
1个回答
展开全部
最明显的错误,就是
No carry operation.
这些最后面都有一个句号。。你的输出没有。
这题是算有多少次进位吧,你是模拟加法运算,
if(maxs[max-1]+mins[min-1]>=106||maxs[max-1]>=58) 这些别人只能猜出来是什么意思,
maxs[max-2]++;这个百分百溢出 。当max = 1的时候 ,即原来的长度是1
没必要用字符串。
#include <iostream>
using namespace std;
int main()
{
unsigned ua, ub;
while(cin >> ua >> ub)
{
if( ua == 0 && ub == 0) break;
// 只要依次抽出最后一位,看下有没有进位就可以。
int ntimes = 0;
int nCarry = 0;
// 循环的条件 ua,ub有一个不为0
while( ua != 0 || ub != 0)
{
unsigned a = ua % 10;
unsigned b = ub % 10;
if( a + b + nCarry >= 10)
{
nCarry = 1;
ntimes ++ ;
}
else
nCarry = 0;
if(ua == 0 && nCarry == 0) break;
if(ub == 0 && nCarry == 0) break; // 如果其中有一个为0,且没有进位,则退出
ua /= 10;
ub /= 10;
}
if( nCarry == 1)
ntimes ++;
if(ntimes == 0)
cout << "No carry operation." << endl;
else if(ntimes == 1)
cout << "1 carry operation." << endl;
else
cout << ntimes << " carry operation." << endl;
}
return 0;
}
追问
哦 超谢谢你 那程序还需要怎么改一下吗
对了 可以要你的联系方式吗 我才入门 求大神带我飞😐
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询