This is an easy problem from uva judge https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=976
- I read the input until I find a 0 0.
- Set a carry variable with 0 at the beginning.
- I divide both number with 10 and I get the module 10 until both numbers are 0.
- In each division I get the last digit of the numbers dividing by 10. I add both last digits and a carry variable. If the value is greater than 9 I set the carry variable with a value different than 0, otherwise I set carry to 0.
- if the carry is greater than 0 I increase the the answer by 1 .
- Print the answer.
- Accepted!
Hints.
- Check all the carries.
Critical Input
999 1
1 999
1 989
989 1
5 5
Critical Output
3 carry operations.
3 carry operations.
1 carry operation.
1 carry operation.
1 carry operation.
Code
/* *Author: Fabian Calsina *Date: 30/06/2016 */ #include <bits/stdc++.h> using namespace std; int main() { int carry; int a,b, ans; while(scanf("%d %d", &a, &b)) { if(!a && !b) break; ans = 0; carry = 0; while(1) { int tmpA = a%10; int tmpB = b%10; if(tmpA + tmpB + carry >= 10 ) { carry = (tmpA + tmpB + carry)/10; } else carry = 0; if(carry) ans++; a/=10; b/=10; //if(carry == 1) // ans++; if(a == 0 && b == 0) break; } if(ans == 0) printf("No carry operation.\n"); else if(ans == 1) printf("1 carry operation.\n"); else printf("%d carry operations.\n", ans); } return 0; }