题目摘要: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).
题目大意:给出f(1)和f(2)以及m的值,根据给出的函数关系,求f(m)
输入输出要求:
Input
Thereare several test cases. Each case contains 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.
输入输出样例:
Sample Input
1 1 3
Sample Output
2.000000
解题思路:可以找出规律,函数值5个一循环
代码:
#include<stdio.h>
double a,b;
int n;
double f(int m)
{
if(m==1)
returna;
elseif (m==2)
returnb;
else
return(f(m-1)+1)/f(m-2);
}
main()
{
doublex;
while(scanf("%lf%lf%d",&a,&b,&n)==3)
{
if(n%5==0)
n=5;
else
n=n%5;
x=f(n);
printf("%.4lf\n",x);
}
return0;
}
解题感想:这种题目很容易想到递归,最开始的思路也是用递归。但是数据太大结果就出错,原因是递归中存在除法。好在这题可以找出规律,函数值5个一循环,只要确定了f(1)和f(2)的值,f(m)就很容易可以得到。