编写函数fun,其功能是:求Fi-bonacci数列中大于t的最小的数,结果由函数返回。Fibonacci数列F(n)的定义为: F(0)=0,F(1)=1 F(n)=F(n-1)+F(n-2) 例如,当t=1000时,函数值为15

admin2020-10-26  16

问题 编写函数fun,其功能是:求Fi-bonacci数列中大于t的最小的数,结果由函数返回。Fibonacci数列F(n)的定义为:
    F(0)=0,F(1)=1
    F(n)=F(n-1)+F(n-2)
    例如,当t=1000时,函数值为1597。
    注意:部分源程序给出如下。
    请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
    试题程序:
1  #include<conio.h>
2   #include<math.h>
3    #include<stdio.h>
4  int fun (int t)
5  {
6
7    }
8  main()
9  {
10    int n ;
11    n=1000;
12   printf(’’n=%d,f=%d\n’’,n,fun(n));
13  }

选项

答案1 int fun(int t) 2 { 3 int f0=0,f1=1,f; 4 do{ 5 /*根据Fiborlacci数列的定义求数值*/ 6 f=f0+f1; 7 f0=f1;f1=f; 8 } while(f
解析 根据所给数列定义不难发现,该数列最终的结果是由两个数列之和组成,所以可以在循环内部始终把f看成是前两项之和,而f0始终代表第n-2项,f1代表第n-1项。退出循环时得到的数f,就是大于指定数的最小的数。
转载请注明原文地址:https://kaotiyun.com/show/Xt3p777K
0

最新回复(0)