您的位置:首页 > 其它

HDU 1568 Fibonacci

2014-03-04 23:45 309 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568

从本题给的n的数据范围来看,这应该是一道推公式的数学题。。。。

用到了Fibonacci数列的通项公式



经过对数运算可得



因为最后一项log10(1-((1-sqrt(5))/(1+sqrt(5)))^n)趋近于0,可以不用计算。

之后取计算得到的小数部分做pow(10.0,小数)的运算,如果不是四位一直*10就ok了。

#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int f[25];
int main()
{
int n;
while(cin>>n)
{
f[0]=0,f[1]=1;
for(int i=2;i<=20;i++)
f[i]=f[i-1]+f[i-2];
if(n<=20) cout<<f
<<endl;
else
{
double t=log10(1.0/sqrt(5))+(double)n*log10((1.0+sqrt(5))/2.0);
t=t-floor(t);
t=pow(10.0,t);
while(t<=1000)
t=t*10;
printf("%d\n",(int)t);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: