倪文迪陪你学蓝桥杯2021寒假每日一题:1.24日(2019省赛A组第2题)

2021年寒假每日一题,2017~2019年的省赛真题。本文内容由倪文迪(华东理工大学计算机系软件192班)和罗勇军老师提供。每日一题,关注蓝桥杯专栏: https://blog.csdn.net/weixin_43914593/category_10721247.html

每题提供C++、Java、Python三种语言的代码。

2019省赛A组第2题,题目链接:
数列求值 http://oj.ecustacm.cn/problem.php?id=1453

1、题目描述

填空题。


给定数列1, 1, 1, 3, 5, 9, 17, …,从第4 项开始,每项都是前3 项的和。求
第20190324 项的最后4 位数字。


2、题解

  倪文迪说:“无”。

3、Python代码

  又是数字!
  Python代码运行时间要好几秒,为什么比下面的C++和Java代码慢那么多?代码几乎一样。

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++代码

   运行时间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代码

   运行时间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);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43914593/article/details/112979520