5038297 :
奇數 : 7 + 2 + 3 + 5 = 17
偶數 : 9 + 8 + 0 = 17
|奇數 - 偶數| = 0
5038297是11的倍數
- #include <iostream>
- using namespace std;
- int main() {
- string s; //待測數字
- while(cin >> s && s != "0") //使用者輸入s,且s不能為0
- {
- long long sum[2] = {0, 0}; //放奇數和和偶數和的陣列
- for(int i = 0;i < s.length();i++)
- {
- sum[i%2] = sum[i%2] + s[i] - '0';
- }
- if(abs(sum[0]-sum[1])%11 == 0)
- {
- cout << s << " is a multiple of 11." << endl;
- }else
- {
- cout << s << " is not a multiple of 11." << endl;
- }
- }
- return 0;
- }
沒有留言:
張貼留言