本文共 2302 字,大约阅读时间需要 7 分钟。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8450 | Accepted: 3166 |
Description
Input
Output
Sample Input
123 456555 555123 5940 0
Sample Output
No carry operation.3 carry operations.1 carry operation.
Source
#include#include #include char num1[20];char num2[20];int main(void){ int add, tmp; int carry; int i, j; int len1, len2; while (scanf("%s %s", num1, num2), strcmp(num1, "0") != 0 || strcmp(num2, "0") != 0){ getchar(); len1 = strlen(num1); len2 = strlen(num2); for (carry = add = 0, i = len1 - 1, j = len2 - 1 ; i >= 0 && j >= 0; i--, j--){ if ((num1[i] - '0') + (num2[j] - '0') + add >= 10){ add = 1; carry ++; } else{ add = 0; } } for (; i >= 0; i--){ if (num1[i] - '0' + add >= 10){ carry++; add = 1; } else { add = 0; } } for (; j >= 0; j--){ if (num2[j] - '0' + add >= 10){ carry++; add = 1; } else { add = 0; } } if (carry == 0){ printf("No carry operation.\n"); } else if (carry == 1){ printf("1 carry operation.\n"); } else{ printf("%d carry operations.\n", carry); } } return 0;}
转载地址:http://aooji.baihongyu.com/