2307 - A easy problem!
时间限制 : 1 秒
内存限制 : 128 MB
<span style="line-height:1.5;"><span style="font-family:'Microsoft Yahei';font-size:14px;line-height:21px;background-color:#F5F5F5;">Define a function f(n)=(f(n-1)+1)/f(n-2). You already got f(1) and f(2). Now, give you a number m, please find the value of f(m).</span></span>
<br />
题目输入
There are several test cases. Each case contains three integers indicating f(1), f(2) and m ( 1 <= f(1), f(2), m <110,000,000).
题目输出
<span style="font-family:'Microsoft Yahei';font-size:14px;line-height:28px;background-color:#F5F5F5;">For each case, please output the value of f(m), rounded to 6 decimal places.</span>
输入/输出样例
输入格式
1 1 3 1 2 3 4 8 10
输出格式
2.000000 3.000000 0.625000
C语言解答
#include <stdio.h> int main() { int m,i,n; double f[11000]; while (scanf("%lf %lf %d",&f[1],&f[2],&m)!=EOF) { for (i=3;i<=100;i++) { f[i]=(f[i-1]+1)/f[i-2]; if (f[i]==f[2]) { if (f[i-1]==f[1]) { n=i-2; break; } } } m=m%n; if (m==0) printf("%.6lf\n",f[n]); else printf("%.6lf\n",f[m]); } return 0; }
C++解答
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; double ans[6]; int main() { int m; // FILE *p,*fp,*f; // fp=fopen("E:\\1.out","at"); // f=fopen("E:\\2.in","r"); // int cnt=0; while(scanf("%lf%lf%d",&ans[1],&ans[2],&m)!=EOF) { // cnt++; ans[3]=(ans[2]+1)/ans[1]; ans[4]=(ans[1]+ans[2]+1)/(ans[1]*ans[2]); ans[0]=(ans[1]+1)/ans[2]; char str[10000]; sprintf(str,"%.6lf",ans[m%5]); puts(str); // fprintf(fp,"%s\n",str); } // printf("%d\n",cnt); //fclose(f); //fclose(fp); return 0; }