题解 UVA10929 【You can say 11】

Title portal
because up to 1000 digits, so use of high-precision or string, of course, in order to facilitate use of the strings.

When the digital number on a digit by subtracting the even bit and odd bit and the difference can be divisible by 11, which is a multiple of 11.

for example:

  1. 2937 and the even bit is 9 + 7 = 16, and the odd bit is 2 + 3 = 5, the difference between the two is 16-5 = 11, so it is a multiple of 11
  2. 112,234 and the even bit is 1 + 2 + 4 = 7, and the odd bit is 1 + 2 + 3 = 6, the difference between the two is 7-6 = 1, so it is not a multiple of 11

Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    while(1)
    {
        cin>>s;
        if(s=="0")return 0;//如果是0则退出
        long long l=0;//l表示总的差
        cout<<s;
        for(int i=0;i<s.size();i++)
        {
            if(i%2)l+=s[i]-'0';else l-=s[i]-'0';//如果是偶数位则加上这个数字,否则减去这个数字
        }
        if(l%11==0)printf(" is a multiple of 11.\n");else printf(" is not a multiple of 11.\n");//判断
    }
}

Guess you like

Origin www.cnblogs.com/pzc2004/p/11600845.html