C语言:各位老兄们,帮我看一下我这段代码哪儿有问题

来源:百度知道 编辑:UC知道 时间:2024/06/22 14:54:53
Description

当你在小学学习算数的时候,老师会教你把两个数由右至左按位加起来。很多时候,加法过程中会出现进位。对于一部分孩子,理解这个“进位”在当时是很困难的事情。现在你的工作就是编写一个程序来判断两个数相加的过程中会产生多少次进位,用以确定这两个数相加的“难度”。

Input

每一行有两个无符号整数(最大不超过1000000000),当输入0 0的时候,程序结束。

Output

对于每一行的输入两个整数,你需要判断会产生有多少个进位,每一个输出占一行。

Sample Input

123 456
555 555
123 594
0 0

Sample Output

No carry operation.
3 carry operations.
1 carry operation.

我的代码:
#include<stdio.h>
int main()
{
long int a,b,m,n,count;
while(1)
{
count=0;
scanf("%d%d",&a,&b);
if(a==0&&b==0)
break;
while(a>0||b>0)
{
m=a%10;
a=a/10;
n=b%10;
b=b/10;
if(m+n>=10)
count++;
}
if(count==0)
printf("No

看看这样

#include<stdio.h>
#include <stdlib.h>
int main()
{
long int a,b,m,n,count;
while(1)
{
count=0;
scanf("%d%d",&a,&b);
if(a==0&&b==0)
break;

int flag=0;
while(a>0||b>0)
{
m=a%10;
a=a/10;
n=b%10;
b=b/10;
if(m+n+flag>=10)
{
flag=1;
count++;
}
else
{
flag=0;
}
}
if(count==0)
printf("No carry operation.\n");
else
printf("%d carry operations.\n",count);
}
system("pause");
return 0;
}

程序中没有考虑
1. 对 a 或 b 小于0的处理
2. 低位进位对高位的影响

另,else if 似不必要

这题明显是让你用数组来代替大数,手工实现加法的过程,期间正好统计出现几次进位.

缺少#include<stdlib.h>文件头!加上就行了!因为你后面使用了system("pause");这个函数!所以要加!我刚刚运行了!没有错误了!

ACM的题么?题目中说的最大不超过1000000000,所以这道