POJ 2562 Primary Arithmetic
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10724 | Accepted: 3980 |
Description
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.
大坑
CODE:
#include <iostream> #include <cstdio> #include <cstdio> #define REP(i, s, n) for(int i = s; i <= n; i ++) #define REP_(i, s, n) for(int i = n; i >= s; i --) #define MAX_N 10 + 5 using namespace std; char a[MAX_N], b[MAX_N]; int int_a[MAX_N], int_b[MAX_N], la, lb; int main(){ while(scanf("%s%s", a + 1, b + 1) != EOF){ if(a[1] == '0' && b[1] == '0') break; memset(int_a, 0, sizeof(int_a)); memset(int_b, 0, sizeof(int_b)); la = strlen(a + 1), lb = strlen(b + 1); REP(i, 1, la) int_a[la - i + 1] = a[i] - '0'; REP(i, 1, lb) int_b[lb - i + 1] = b[i] - '0'; int i = 1, x = 0, res = 0; while(i <= la || i <= lb){ x = int_a[i] + int_b[i] + x; x /= 10; if(x != 0) res ++; i ++; } if(res == 0) printf("No carry operation.\n"); else if(res == 1) printf("%d carry operation.\n", res); else printf("%d carry operations.\n", res); } return 0; }