您的位置:首页 > 其它

hdu 1568 Fibonacci

2015-05-25 21:01 393 查看
题意:

输出第n个 Fibonacci数的前四位



解题思路:

Fibonacci数列的公式




用log做,举个例子,log10(1007) = 3+log10(1.007),那么令a = log10(1.007)= 0.00302947

10^a = 1.007

那么可先求b =


然后求出b的小数部分,取出10^b的前四位即可

#include <iostream>
#include <math.h>
using namespace std;
int f[21];
int main()
{
	int n;
	f[0] = 0;
	f[1] = 1;
	for(int i=2;i<=20;i++) f[i] = f[i-1]+f[i-2];
	while(cin>>n)
	{
		if(n<=20) 
		{
			cout<<f
<<endl;
			continue;
		}
		double a = log10((1*1.0)/sqrt(5*1.0));
		double b = n*(log10((1+sqrt(5*1.0))*1.0/2))+a;
		b = b-floor(b);
		b = pow(10,b);
		int c = (int)(b*1000);
		cout<<c<<endl;
	}
	return 0;
}










内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: