E.Function(13.7.8)

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表示保留的小数个数




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值