The problem of divisibility by 11. C++

Task condition :

For a number to be divisible by 11, the difference between the sum of the digits in even places and the sum of the digits in odd places must be divisible by 11.

You need to write a program that checks the divisibility of a given number by 11.

Input data

Input file INPUT.TXT contains a single natural number N, the divisibility of which must be checked (1 ≤ N ≤ 10^10000).

Weekend data

To the output file OUTPUT.TXT output "YES" if the number is divisible by 11, or "NO" otherwise.

I'll make a reservation right away. The site allows you to do normal input and output without using file streams.

Decision:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long even = 0, odd = 0;
    string inp;
    cin >> inp;
    int size = inp.size();
    for (size_t i = 0; i < size; i += 2)
        even += inp[i] - '0';
    for (size_t i = 1; i < size; i += 2)
        odd += inp[i] - '0';
    cout << (even - odd % 11 ? "NO" : "YES");
}

On one of the tests, the program fails (the tests are not visible to me). And in fact, I don't even understand why my program is failing.

Overflow?

Well consider the number 10^1000-1, this number is it consists of 9999 nines. for each odd and even variable, the distribution of nines is approximately equal. but in the case of an odd number of digits, the odd outweighs. so in the extreme case odd = 9*(9998/2 + 1) = 45000. I put a long long out of fear, but obviously it didn't help me.

Loop variable overflow?

Size_t range = 2^16-1, and in fact 1000 is already enough for me, because the string length is up to 10^1000

Maybe we should have used the odd - even condition, not even - odd?

My condition requires a non-zero remainder and in this case my condition is true. Even if the balance is negative, it will not affect the work in any way.

And in general, I am completely convinced of the correctness of my decision. Out of desperation, I decided to test its performance on numbers up to 10e7. And everything worked quite adequately.

I generally then thought that maybe the selection of the line is a bad idea. So I decided to make a second decision. Which is not working software at all, too, not for reasons I understand.

Second solution:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long even = 0, odd = 0;
    char x, i = 0;
    while ((x = cin.get()) != EOF)
    {
        if (i % 2 == 0)
            even += x - '0';
        else
            odd += x - '0';
        i++;
    }
    cout << (even - odd % 11 == 0 ? "YES" : "NO");
} 

I'm probably missing some special case. But the second program definitely works somehow crookedly. Please explain to me what I am missing and what I have done wrong in my programs.

Author: Grundy, 2020-06-26

2 answers

Everything is much simpler. You are given a line with a final line feed... cin.get() reads it.

Here is a solution that passes the tests:

#include <iostream>
int s;
char a;

int main(){
    while(std::cin>>a)
    {
        s+=a;
        s-=std::cin>>a?a:'0';
    }
    std::cout << (s%11 ? "NO":"YES");
}

By the way, as you can see, there is no need to keep two amounts separately.

 5
Author: Harry, 2020-06-27 04:05:23

If you don't even look closely, then:

Your program enters characters, and these characters can be anything, including zeros at the beginning, or a non-digit can be entered. Therefore, you will have to check whether the first character is greater than or equal to the character '1' and less than or equal to ' 9 'or the sign of the number' +'(then you need to skip it) and other digits can also be zero.

Second: You are using a console application where there may not be enough characters per line (for example, if we want to enter a number greater than 10 ^ 100 ), and you practically read only the first line (up to the character '\n'), which means not the entire digit, and in the second example you read '\n ' too, which is even worse.

Third:

You go through all the characters of the string twice, when you can do everything at once:

 0
Author: AR Hovsepyan, 2020-06-27 05:22:54