Blue Bridge Cup - 2019 C ++ A first set of questions: square and [enumeration]

I, entitled

        Xiaoming of 2,0,1,9 digit numbers contained very interested in 1 to 40 comprising a number of such 1,2,9,10 and 40 to 32, 39, a total of 28, and they are 574 , and the square is 14362. Note that the sum of squares means after each number separately summing the squares.   

        Ask, in the 1 to 2019, all such square of the number and how much?

        This is a result of fill in the blank questions, you only need to submit to the calculated result. The results of this question is an integer, when submitting answers only to fill in the integer, fill in the extra content will not score.

        Tip: If you write a program calculations and found that the result is negative, please double-check your program, I do not doubt the examination of the programming software.

Second, the idea

Write a function to view each number 1-2019 each digit if there are four numbers 2,0,1,9, and if so, record this number. And finally obtained the square.

Pay attention to use long long, will be out of bounds by int.

Third, the problem solution

#include <iostream>
using namespace std;
bool check(long long n)
{
    while(n)
    {
        int t= n%10;
        if(t==2 || t==0 || t==1 || t==9)
        {
            return true;
        }
        n=n/10;
    }
    return false;
}

int main() {
    long long  ans=0;
    for (long long  i=1;i<=2019;i++)
    {
        if(check(i))
        {
            ans=ans+i*i;
        }
    }
    cout << ans << endl;
    return 0;
}

 

Fourth, the results

2658417853

Process finished with exit code 0

Published 57 original articles · won praise 9 · views 3578

Guess you like

Origin blog.csdn.net/Jayphone17/article/details/104263573