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

題目

判斷輸入進來的數字(可能大到 1000 個位數)是否為 11 的倍數。

UVa 連結
ZeroJudge 連結

解法

11 的倍數判別法:「奇數位和」減「偶數位和」是 11 的倍數。

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

int main(){
    string s;
    while(cin >> s && s != "0"){
        int odd = 0, even = 0;
        for(int i=0; i<s.length(); 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大學程式能力檢定】目錄
發佈留言

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