2019第十届蓝桥杯C/C++ B组省赛 —— 第三题:数列求值

试题 C:数列求值

本题总分:10 分

【问题描述】

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

【答案提交】

   这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一 个 4 位整数(提示:答案的千位不为 0),在提交答案时只填写这个整数,填写多余的内容将无法得分。

代码

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    long long a=1,b=1,c=1,num=0;
    for (int i = 4; i <= 20190324; ++i) {
        num=(a+b+c)%10000;
        c=b;b=a;a=num;
    }
    cout<<num<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43336281/article/details/89309932