就业数据资源平台
当前位置:首页 > C语言程序设计
计算机二级C语言上机考试编程题及答案3

编程题
  请编写函数fun,它的功能是:求Fibonacci数列中大于t(t>3)的最小的一个数,结果由函数返回.其中Fibonacci数列F(n)的定义为:
  F(0)=0,F(1)=1
  F()=F(n-1)+F(n-2)
  假如:当 t=1000时, 函数值为1567.
  请勿改动主函数动main其它的函数中的任何内容,仅在函数fun花括号中填入所编写的若干语句.
  #include <conio.h>
  #include <math.h>
  #include <stdio.h>
  int fun (int t)
  {
  } 来源:考试大
  main()
  {
  int n;
  FILE *out;
  n=1000;
  printf("n=%d,f=%d\n",n,fun(n));
  out=fopen ("out.dat", "w");
  for (n = 500; n < 3000; n+=500)
  fprintf(out, "%d\n", fun(n));
  fclose (out );
  }
  参考答案:
  int fun(int t)
  {
  int a=1,b=1,c=0,I;
  for(i=4;i<=t;i++)
  {
  if(c<t)
  {
  c=a+b;
  a=b;
  b=c;
  }
  else
  break;
  }
  return c:
  }
就业数据资源平台