1064. 计算斐波那契第n项 通项公式

题目描述

输入n,编写程序输出斐波那契数列的第n项。其中斐波那契数列f(n)的定义如下:
f(1)=0,f(2)=1        
f(n)=f(n-1)+f(n-2)(n>=2)

输入

一行一个正整数n。

输出

 输出一个数f(n)。

样例输入

5

样例输出

3

数据范围限制

1<=n<=30
n---------------------------------------------------------------
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     cin>>n;
 9     n--;
10     double x=sqrt(5.0);
11     cout<<(int)(((pow((1+x)/2.0,n)-(pow((1-x)/2.0,n))))*x)/5.0;
12     return 0;
13 }
原文地址:https://www.cnblogs.com/zwfymqz/p/6675097.html