【1188】Fibonacci数列(2)

【问题描述】
       Fibonacci数列是指这样的数列: 数列的第一个和第二个数都为1,接下来每个数都等于前面2个数之和。
       给出一个正整数a,要求Fibonacci数列中第a个数对1000取模的结果是多少。。
【输入】
       第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a(1 ≤ a ≤ 1000000)。
【输出】
       n行,每行输出对应一个输入。输出应是一个正整数,为Fibonacci数列中第a个数对1000取模得到的结果。
【输入样例】
       4
       5
       2
       19
       1
【输出样例】
       5
       1
       181
       1
【参考程序】

#include <cstdio>
#include <iostream>
using namespace std;

int n, tot, a[1000001]={0,1,1};
int main() {
	for (int i=3; i<=1000000; i++) {
		a[i] = (a[i-1] + a[i-2]) % 1000;		// 避免超出1000的范围 
	}
	
	cin >> tot;
	while (tot--) {
		cin >> n;
		cout << a[n] << endl;					// 输出第n额Fibonacci数 
	} 
	
	return 0;
}
发布了66 篇原创文章 · 获赞 0 · 访问量 1210

猜你喜欢

转载自blog.csdn.net/developer_zhb/article/details/105609819