题解 | Primary Arithmetic

Primary Arithmetic

https://www.nowcoder.com/practice/c1fb44e931394e6693671f49c899f5de

#include <iostream>
using namespace std;

int main() {
    unsigned long a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        int carry=0,sum=0,cnt=0;
        if(a==0&&b==0)break;;
        while(a>0||b>0){
            sum=(a%10)+(b%10)+carry;
            if(sum>=10){
                carry=1;
                cnt++;
            } else carry=0;
            a=a/10;
            b=b/10;
        }
        if(cnt==0)printf("NO carry operation.\n");
        else if(cnt>1)printf("%d carry operations.\n",cnt);
        else printf("1 carry operation.\n");
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务