题目描述
Children are taught to add multi-digit numbers from right to left, one digit at a time. Many find the “carry" operation, where a 1 is carried from one digit position 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, compute the number of carry operations that result from adding the two numbers and print them in the format shown below.
样例输入
123 456
555 555
123 594
0 0
样例输出
No carry operation.
3 carry operations.
1 carry operation.
参考代码
#include <stdio.h>
#include <string.h>
void exchange( char a[], int b[] );
int main()
{
int a[20], b[20];
char c[20], d[20];
int i;
int count;
int t1, t2;
while( scanf("%s%*c%s", c, d) != EOF )
{
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
count = 0;
if( c[0] == '0' && d[0] == '0' )
{
break;
}
exchange(c, a);
exchange(d, b);
t1 = strlen(c);
t2 = strlen(d);
t1 = t2 > t1 ? t2 : t1;
for ( i = 0; i < t1+1; i++ )
{
if( a[i] + b[i] > 9 )
{
a[i+1] += 1;
count++;
}
}
if(count == 0)
{
printf("No carry operation.n");
}
if(count == 1)
{
printf("1 carry operation.n");
}
if(count > 1)
{
printf("%d carry operations.n",count);
}
}
return 0;
}
void exchange( char a[], int b[] )
{
int i;
for( i= strlen(a)-1 ; i >= 0; i-- )
{
b[strlen(a)-1-i] = a[i] - '0';
}
}
解析
暂无