iT邦幫忙

2024 iThome 鐵人賽

DAY 11
0
佛心分享-刷題不只是刷題

CPE C++ 刷題系列 第 11

CPE C++ 刷題 Day 11

  • 分享至 

  • xImage
  •  

今天來解YKL11(UVA10035):Primary Arithmetic

Primary Arithmetic

https://ithelp.ithome.com.tw/upload/images/20240925/20155574veQY5H62nO.png

mod取餘數
計算有幾個進位

#include <iostream>
#include <string>
using namespace std;

int main(){
    int a,b;
    while(cin >> a >> b){
        int count = 0,carry = 0;
        if(a==0 && b ==0) break;
        while(a > 0 || b > 0){
            int dig1 = a % 10;
            int dig2 = b % 10;

            int sum = dig1 + dig2 + carry;

            if(sum >= 10){
                carry += 1;
                count++;
            }else{
                carry = 0;
            }
            a /= 10;
            b /= 10;
       

        }
        if(count == 0) cout << "No carry operation." << endl;
        else if(count == 1) cout << "1 carry operation." << endl;
        else cout << count << " carry operations." << endl;

    }
    return 0;
}

上一篇
CPE C++ 刷題 Day 10
下一篇
CPE C++ 刷題 Day 12
系列文
CPE C++ 刷題20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言