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

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 2, Question link:
Sequence evaluation http://oj.ecustacm.cn/problem.php?id=1453

1. Title description

Fill in the blanks.


Given a sequence of 1, 1, 1, 3, 5, 9, 17, …, starting from item 4, each item is the sum of the first three items. Find
the last 4 digits of item 20190324.


2. Problem solution

  Ni Wendi said: "Nothing."

3. Python code

  Numbers again!
  Python code takes several seconds to run. Why is it so much slower than the following C++ and Java code? The code is almost the same.

a,b,c = 1,1,1
for i in range(4,20190325):
    y=(a+b+c)%10000
    a=b
    b=c
    c=y
print(y)

4. C++ code

   Running time is 200ms.

#include <bits/stdc++.h>
using namespace std;
int main(){
    
    
   int a=1,b=1,c=1,y;
   for(int i=4;i<=20190324;i++){
    
    
       y=(a+b+c)%10000;
       a=b; b=c;c=y;
   }
   cout<<y;
}

5. Java code

   Running time is 200ms.

public class Main {
    
    
    public static void main(String[] args) {
    
    
        int a=1, b=1, c=1, y=0;
        for(int i=4;i<=20190324;i++) {
    
    
            y=(a+b+c)%10000;
            a=b; b=c; c=y;
        }
        System.out.println(y);
    }
}

Guess you like

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