題目
判斷輸入進來的數字(可能大到 1000 個位數)是否為 11 的倍數。
解法
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大學程式能力檢定】目錄