AcWing 741. 斐波那契数列
原题链接
简单
作者:
dandelion7
,
2024-02-23 12:36:47
,
所有人可见
,
阅读 29
yxc solution
注意:整数溢出
long long 类型输出是 lld
用数组存储斐波那契数列
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
long long f[61];
f[0] = 0, f[1] = 1;
for (int i = 2; i <= 60; i ++ ) f[i] = f[i - 1] + f[i - 2];
int n;
cin >> n;
while (n -- )
{
int x;
cin >> x;
printf("Fib(%d) = %lld\n", x, f[x]);
}
return 0;
}
my solution
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
double a=0, b=1;
for(int i=0; i<n; i++){
double temp=a+b;
a=b;
b=temp;
}
printf("Fib(%d) = %.0lf\n", n, a);
}
return 0;
}