文章
454
粉丝
0
获赞
1
访问
10.5k
每行两个非负整数,统计竖式加法中的进位次数,按单复数输出;0 0 结束。
从最低位开始逐位相加,维护进位标志 c,和为 10 及以上就进位并计数。
输出格式三种:没有进位输出 NO carry operation.,一次进位输出 1 carry operation.,多次输出 N carry operations.。
0 0 可能是组间分隔也可能是结尾,读到就跳过继续读,不能直接结束程序。
O(位数)。
C:
#include <stdio.h>
int main(void) {
unsigned long long a, b;
while (scanf("%llu %llu", &a, &b) == 2) {
int cnt = 0, c = 0;
if (a == 0 && b == 0) continue;
while (a > 0 || b > 0) {
int s = (int)(a % 10) + (int)(b % 10) + c;
if (s >= 10) { c = 1; cnt++; } else c = 0;
a /= 10;
b /= 10;
}
if (cnt == 0) printf("No carry operation.\n");
else if (cnt == 1) printf("1 carry operation.\n");
else printf("%d carry operations.\n", cnt);
}
return 0;
}
C++:
#include <stdio.h>
int main(void) {
unsigned long long a, b;
while (scanf("%llu %llu", &a, &b) == 2) {
int cnt = 0, c = 0;
...
登录后发布评论
暂无评论,来抢沙发