【題解】CPE一顆星選集第5題:10929 – You can say 11

題目簡述:判斷輸入進來的數字(可能大到 1000 個位數)是否為 11 的倍數。
題目 / ZJ

此題重點:11 的倍數判別法為「奇數位數字和」減 「偶數位數字和」等於 0 或 11 的倍數。

#include <iostream>
#include <string>
using namespace std;
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    string s;
    while(cin >> s){
        if(s == "0") break;
        int odd = 0, even = 0, n = s.length();
        for(int i=0; i<n; i++){
            if(i % 2) odd += s[i] - '0';
            else even += s[i] - '0';
        }
        if((odd - even) % 11 == 0){
            cout << s << " is a multiple of 11.\n";
        }else{
            cout << s << " is not a multiple of 11.\n";
        }
    }
}
👉 回到:【CPE大學程式能力檢定】目錄
發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *