-
Title Description:
-
We all know that the Fibonacci sequence now requires you to enter an integer n, and you output the nth of the Fibonacci sequence. The Fibonacci sequence is defined as follows:
-
Input:
-
The input may contain multiple test samples, for each test case,
The input includes an integer n (1<=n<=70).
-
Output:
-
corresponding to each test case,
Outputs the value of the nth Xiang Feipo sequence.
-
Sample input:
-
3
-
-
Sample output:
-
2
Note: This topic uses recursion to do a lot of repetitive calculations, the efficiency is very low.
Code:
#include <iostream>using namespacestd;Long LongFibonacci (intN) { if(n==0){ return 0; } if(n==1){ return 1; } Long Longprenum=0; Long Longcurnum=1; Long LongAnsnum; for(intI=2; i<=n;++i) {Ansnum=prenum+Curnum; Prenum=Curnum; Curnum=Ansnum; } returnAnsnum;} intMain () {intN; while(cin>>N) {cout<<fibonacci (n) <<Endl; } return 0;} /************************************************************** problem:1387 User:lcyvino language:c++ Re sult:accepted time:10 Ms memory:1520 kb****************************************************************/
The Fibonacci sequence of the offer