斐波那契 |
时间限制:1000/1000 MS(Java / Others)内存限制:32768/32768 K(Java / Others) |
提交的总数:4869接受的提交:2199 |
问题描述 2007年到来了。经过2006年一年的修炼,数学神童zouyu终于把0到100000000的斐波纳契数列 |
输入 输入若干数字n(0 <= n <= 100000000),每个数字一行。读到文件尾。 |
产量
|
样本输入
扫描二维码关注公众号,回复:
4526755 查看本文章
|
样本输出 |
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
double cnt = (1 + sqrt(5)) / 2.0;
int a[21] = { 0,1 };
int main()
{
int n;
double ans;
for (int i = 2; i < 21; i++)
a[i] = a[i - 1] + a[i - 2];
while (cin>>n)
{
if (n < 21)
printf("%d\n", a[n]);
else
{
ans = -log10(sqrt(5))+ n * log10(cnt);
ans = ans - floor(ans);
ans = pow(10, ans);
ans = floor(ans * 1000);
printf("%.0lf\n", ans);
}
}
system("pause");
return 0;
}