hdu 1568フィボナッチ数列
1769 ワード
http://acm.hdu.edu.cn/showproblem.php?pid=1568
Problem Description
2007年が来ました.2006年の1年間の修練を経て、数学の神童zouyuはついに0から100000万までのFibonacciを数列に並べました.
(f[0]=0,f[1]=1;f[i]=f[i-1]+f[i-2](i>=2)の値は全部暗記しました.
続いて、CodeStarは彼を試験することを決定して、そこで彼に1つの数字を聞いて、彼は解答を言い出して、でもある数字は長すぎます.4位を超えるのはベスト4を言えばいいと規定されていますが、CodeStar自身は覚えられません.そこで彼はプログラムを作成して、zouyuの言うことが正しいかどうかをテストすることにしました.
Input
いくつかの数字n(0<=n<=100000万円)を入力し、各数字の1行を指定します.ファイルの最後まで読みます
Output
f[n]の前の4つの数字を出力します.4つの数字が足りないなら、全部出力します.
Sample Input
Sample Output
Problem Description
2007年が来ました.2006年の1年間の修練を経て、数学の神童zouyuはついに0から100000万までのFibonacciを数列に並べました.
(f[0]=0,f[1]=1;f[i]=f[i-1]+f[i-2](i>=2)の値は全部暗記しました.
続いて、CodeStarは彼を試験することを決定して、そこで彼に1つの数字を聞いて、彼は解答を言い出して、でもある数字は長すぎます.4位を超えるのはベスト4を言えばいいと規定されていますが、CodeStar自身は覚えられません.そこで彼はプログラムを作成して、zouyuの言うことが正しいかどうかをテストすることにしました.
Input
いくつかの数字n(0<=n<=100000万円)を入力し、各数字の1行を指定します.ファイルの最後まで読みます
Output
f[n]の前の4つの数字を出力します.4つの数字が足りないなら、全部出力します.
Sample Input
0
1
2
3
4
5
35
36
37
38
39
40
Sample Output
0
1
1
2
3
5
9227
1493
2415
3908
6324
1023
詳細は:http://blog.csdn.net/lvshubao1314/article/details/38013897 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;
int a[25];
int main()
{
a[0]=0;
a[1]=1;
for(int i=2;i<22;i++)
a[i]=a[i-1]+a[i-2];
//printf("%d
",a[21]);
int n;
while(~scanf("%d",&n))
{
if(n<=20)
{
printf("%d
",a[n]);
continue;
}
else
{
double log_s=log10(1.0/sqrt(5.0)) +(double)n*log10((1.0+sqrt(5.0))/2.0);
int ans_s=(int)(pow(10.0,log_s-(int)log_s+3));
cout <<ans_s<<endl;
}
}
return 0;
}