整数进位

来源:互联网 发布:淘宝助手图片搬家失败 编辑:程序博客网 时间:2024/05/11 19:37

求两个数在相加时需要多少次进位。输入的数字不超过9个数字。


#include<iostream>#include<cstdio>#include<algorithm>#include<string.h>using namespace std;int main(){int a, b, i, tmp, ans;while(scanf("%d%d", &a, &b) && a && b){ans = 0;for(i = 9; i >= 0; --i){tmp = (a % 10 + b % 10) > 9 ? 1 : 0;ans += tmp;a /= 10; b /= 10;}printf("%d\n", ans);}return 0;}

int的上限大概是20亿,可以保存9位数字。

原创粉丝点击