ZeroJudge D235: You can say 11

UVa 10929 – You can say 11

Your task is to determine whether a given positive integer N is a multiple of 11.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
EOF inputs: each line of data contains a positive integer N, where N can have up to 1000 digits.
If N = 0, it means the input has ended.
對每個輸入的數,輸出是否為 11 的倍數。輸出格式請參考 Sample Output。
112233
30800
2937
323455693
5038297
112234
0
112233 is a multiple of 11.
30800 is a multiple of 11.
2937 is a multiple of 11.
323455693 is a multiple of 11.
5038297 is a multiple of 11.
112234 is not a multiple of 11.

Thought Process

Since the numbers in this problem can exceed the range of a Long Long Int, we'll use strings to handle the input.

We'll iterate through each digit of the number, summing up the digits at odd and even positions separately. Then, we'll check if the absolute difference between these sums is a multiple of 11 to determine if the number is divisible by 11. Let's implement this logic.

Sample Code-ZeroJudge D235: You can say 11

#include <iostream>
using namespace std;

int main() {
    string str;
    while (cin >> str && str != "0")
    {
        int odd = 0, even = 0;
        for (int i = 0; i<str.length(); i++)
        {
            if (i % 2 == 0) even += int(str[i] - '0');
            else odd += int(str[i] - '0');
        }
        if ((odd - even) % 11 == 0) cout << str << " is a multiple of 11.\n";
        else cout << str << " is not a multiple of 11.\n";
    }
}

//ZeroJudge D235
//Dr. SeanXD

Comments