Function解题报告
2013-07-10 20:19
183 查看
题目摘要: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)就很容易可以得到。
题目大意:给出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)就很容易可以得到。
相关文章推荐
- LeetCode 396. Rotate Function 解题报告
- URAL 1010 Discrete Function(解题报告)
- 解题报告 Function
- [Leetcode] 396. Rotate Function 解题报告
- Leetcode 396. Rotate Function 旋转方程 解题报告
- HDU-2824- The Euler function 解题报告
- 解题报告:UVA11395 Sigma Function 找规律
- [leetcode] 396. Rotate Function 解题报告
- [解题报告]Ural 1010 Discrete Function
- URAL 1010 Discrete Function(解题报告)
- ZOJ 3772 Calculate the Function 解题报告
- Leetcode Insertion Sort List 解题报告
- BZOJ 4027 [HEOI 2015] 兔子与樱花 解题报告
- [leetcode] 155. Min Stack 解题报告
- [Leetcode] 369. Plus One Linked List 解题报告
- openoj的一个小比赛(J题解题报告)poj1703(并查集)
- hdu 2446 二分搜索解题报告
- UVA 12589 Learning Vector 解题报告
- CUGBACM_Summer_Tranning 组队赛解题报告
- poj图论解题报告索引