Problem Description
source:https://uva.onlinejudge.org/external/100/10035.html
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.
Input
Each line of input contains two unsigned integers less than 10 digits. The last line of input contains ‘0 0’.
Output
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
Sample Output
No carry operation.
3 carry operations.
1 carry operation.
Solution:
source:https://uva.onlinejudge.org/external/100/10035.html
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.
Input
Each line of input contains two unsigned integers less than 10 digits. The last line of input contains ‘0 0’.
Output
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
Sample Output
No carry operation.
3 carry operations.
1 carry operation.
Solution:
#include<stdio.h> int main() { long int digit1, digit2,carry, count,mod1,mod2,sum,l,big; while(scanf("%ld %ld",&digit1, &digit2)==2) { if(digit1==0 && digit2==0) break; if(digit1>digit2) big=digit1; else big=digit2; l=0; while(big!=0) /* count the length of big number*/ { big/=10; l++; } count=0; carry=0; while(l!=0) { mod1=digit1%10; digit1=digit1/10; mod2=digit2%10; digit2=digit2/10; sum=mod1+mod2+carry; if(sum>9) { count++; carry=sum/10; } else carry=0; l--; } if(count==0) printf("No carry operation.\n"); else if(count==1) printf("1 carry operation.\n"); else printf("%ld carry operations.\n",count); } return 0; }
No comments:
Post a Comment
Write your comment - Share Knowledge and Experience