Ni Wendi will accompany you to learn the Blue Bridge Cup 2021 Winter Holiday. One question per day: 1.23 (2019 Provincial Competition Group A Question 1)

There will be one question every day during the winter vacation of 2021, and the real question for the provincial competition from 2017 to 2019. The content of this article was provided by Ni Wendi (Class 192, Computer Department, East China University of Science and Technology) and Luo Yongjun. One question a day, follow the Blue Bridge Cup column: https://blog.csdn.net/weixin_43914593/category_10721247.html

Each question provides code in three languages: C++, Java and Python .

2019 Provincial Competition Group A Question 1 " Sum of Squares ", title link:
http://oj.ecustacm.cn/problem.php?id=1452

1. Title description

Fill in the blanks.


Xiaoming of 2,0,1,9 digit numbers contained very interested in 1 to 40 the number of such packets
include 1,2,9,10 to 32, 39 and 40, a total of 28, and they are 574, the sum of squares is 14362.
Note that the sum of squares means that each number is squared separately and then summed.
May I ask, from 1 to 2019, what is the sum of squares of all such numbers?


2. Problem solution

  no.

3. Python code

   Great, it's numbers again. Python is the most convenient.

sum = 0
for i in range(1,2020):
    s = str(i)
    if '2' in s or '0' in s or '1' in s or '9' in s:
         sum += i*i
print(sum)

4. C++ code

   C++ should be coded according to the rules.

#include<bits/stdc++.h>
using namespace std;
bool check(int n){
    
    
    while(n){
    
    
        if(n%10==1||n%10==2||n%10==9||n%10==0)
            return true;
        n/=10;
    }
    return false;
}
int main(){
    
    
    long long sum=0;
    for(int i=1;i<=2019;i++)
        if(check(i))
            sum+=i*i;
    cout<<sum;
}

5. Java code

   Java can also be like Python, but the code is a bit too annoying.

public class Main{
    
    
    public static void main(String[] args) {
    
    
        long sum=0;
        for(int i=1;i<=2019;i++) {
    
    
            String s = String.valueOf(i);
            if(s.contains("2") || s.contains("0") || s.contains("1") || s.contains("9"))
                sum += i*i;
        }
        System.out.println(sum);
    }
}

Guess you like

Origin blog.csdn.net/weixin_43914593/article/details/112979396