Primary Arithmetic
来源:BNUOJ 1006
http://www.bnuoj.com/v3/problem_show.php?pid=1006
当你在小学学习算数的时候,老师会教你把两个数由右至左按位加起来。 很多时候,加法过程中会出现进位。对于一部分孩子,理解这个“进位” 在当时是很困难的事情。现在你的工作就是编写一个程序来判断两个数 相加的过程中会产生多少次进位,用以确定这两个数相加的“难度”。 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. Source 第七届北京师范大学程序设计竞赛热身赛第一场 (1)注意输出结果里面的细微区别: operations和operation (2)这个题目大多数人可能会想:直接把两个数当字符串输入再从后往前扫描处理。 其实这个题可以重复下面的过程:取余数直接得到最低位,然后消掉最低位。
1 #include<stdio.h> 2 int main() 3 { 4 int a,b,x,y,c; 5 int result; 6 freopen("input.txt","r",stdin); 7 while(scanf("%d%d",&a,&b)!=EOF) 8 { 9 if(a==0&&b==0) break; 10 result=0; 11 c=0; 12 while(a>0||b>0) 13 { 14 x=a%10; 15 y=b%10; 16 a=a/10; 17 b=b/10; 18 c=x+y+c; 19 if(c>=10) 20 { 21 result++; 22 c=c/10; 23 } 24 else 25 { 26 c=0; 27 } 28 } 29 if(result==0) printf("No carry operation.\n"); 30 else 31 { 32 if(result>1) printf("%d carry operations.\n",result); 33 else printf("%d carry operation.\n",result); 34 } 35 } 36 return 0; 37 }