E.Function
Define a functionf(n)=(f(n-1)+1)/f(n-2). You already got f(1) and f(2). Now, give you a numberm, please find the value of f(m).
Input
There are several test cases. Each casecontains three integers indicating f(1), f(2) and m ( 1 <= f(1), f(2), m<= 1000,000,000).
Output
For each case, please output the value off(m), rounded to 6 decimal places.
SampleInput
1 1 3
SampleOutput
2.000000
这个题目中给出了上一个与下一个的关系,看似递归是很简单的,但是使用递归后,出现了很多问题
代码:
#include<iostream>
#include<iomanip>
using namespace std;
float f(float a,float b,int n){
if(n==1){return a;}
else if(n==2){return b;}
else{return ((f(a,b,n-1)+1)/f(a,b,n-2));}
}
int main()
{
float a,b;
int m;
while(cin>>a>>b>>m){
cout<<setiosflags(ios::fixed)<<setprecision(4)<<f(a,b,m)<<endl;}
return 0;
}
在提交这个代码时,会出现内存超标,再进行调试多次后,仍然是无法提交,早本地运行可以得到答案,妥妥的,同时在此代码中,必须学会使用用C++保留有效小数个数首先包括头文件<iomanip>然后在输出时加上setiosflags(ios:fixed)<<setprecisions(x);x表示保留的小数个数