前言
蓝桥杯2019省赛,填空题(C++)
一、题目描述
给定数列 1, 1, 1, 3, 5, 9, 17,⋯,从第 4 项开始,每项都是前 3 项的和。
求第 20190324 项的最后 4 位数字。
运行限制
- 最大运行时间:1s
- 最大运行内存: 128M
二、思路
这个题可以通过暴力的方法来解决,就像斐波那契那样,将其装入for循环中。因为结果要求输出最后的4位数字,所以我们每次相加的时候,需要对三个操作数,假定为a,b,c 进行a%=10000,b%=10000,c%=10000。最后输出结果即可。
答案:4659
三、具体代码
#include<bits/stdc++.h>
using namespace std;
long long count_num=4;
unsigned long long temp=0;
void dfs(int a,int b,int c)
{
temp=a+b+c;
count_num++;
cout<<count_num<<" "<<temp<<endl;
if(count_num==1000)
{
return;
}
a=b;
a=a&16383;
b=c&16383;
c=temp;
c=c&16383;
dfs(a,b,c);
}
int main()
{
int a=1,b=1,c=1;
long long n=20190324;
for(long long i=4;i<=n;i++)
{
temp=a+b+c;
a=b;
a=a%10000;
b=c;
b=b%10000;
c=temp;
c=c%10000;
}
cout<<c<<endl;
return 0;
}