Primary Arithmetic
题目描述
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.
样例输入
123 456
555 555
123 594
0 0
样例输出
No carry operation.
3 carry operations.
1 carry operation.
题目翻译
解析:
- 首先,用while循环,输入m,n;
- 其次,结合大整数加法(见前文),开始累加;
- 最后,输出结果。
代码如下:
#include<bits/stdc++.h>
using namespace std;
char sa[10001],sb[10001];
int main()
{
int m,n;
while(cin>>m>>n&&(m||n))
{
int a[10001]={0},b[10001]={0},c[10001]={0},la,lb,d=0,s,t,i=0,j,k,p=0;
while(m!=0)
{
a[i]=m%10;
m/=10;
i++;
}
la=i;
i=0;
while(n!=0)
{
b[i]=n%10;
n/=10;
i++;
}
lb=i;
k=la>lb?la:lb;
i=0;d=0;
while(i<k)
{
s=a[i]+b[i]+d;
c[i]=s%10;
d=s/10;
if(d)
{
p++;
}
i++;
}
if(p==1)
cout<<"1 carry operation."<<endl;
if(p>1)
cout<<p<<" carry operations."<<endl;
if(p==0)
cout<<"No carry operation."<<endl;
}
return 0;
}