70 lines
1.2 KiB
C++
70 lines
1.2 KiB
C++
/*------------------------------------------------
|
||
【程序设计】
|
||
--------------------------------------------------
|
||
/* 编写函数fun,它的功能是:求Fibonacci数列中大于s的最小的一个数,结果由函数返回。其中Fibonacci数列F(n)的定义为:
|
||
F(0)=0,F(1)=1
|
||
F(n)=F(n-1)+F(n-2)
|
||
例如:当s=1000时,函数值为:1597。
|
||
注意:部分源程序存在文件prog.c中。
|
||
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
|
||
*********Begin**********和********** End **********不可删除
|
||
*/
|
||
|
||
|
||
|
||
#include <conio.h>
|
||
#include <math.h>
|
||
#include <stdio.h>
|
||
#include<windows.h>
|
||
int fun(int s)
|
||
{
|
||
/********** Begin **********/
|
||
int f1,f2,f;
|
||
f1=0;
|
||
f2=1;
|
||
do
|
||
{
|
||
f=f1+f2;
|
||
f1=f2;
|
||
f2=f;
|
||
}
|
||
while(f2<=s);
|
||
|
||
return f2;
|
||
|
||
|
||
|
||
|
||
|
||
/********** End ***********/
|
||
|
||
}
|
||
|
||
|
||
void NONO ( )
|
||
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
|
||
FILE *fp, *wf ;
|
||
int i, n, s ;
|
||
|
||
fp = fopen("bc06.in","r") ;
|
||
if(fp == NULL) {
|
||
printf("数据文件bc06.in不存在!") ;
|
||
}
|
||
wf = fopen("bc06.out","w") ;
|
||
for(i = 0 ; i < 10 ; i++) {
|
||
fscanf(fp, "%d", &n) ;
|
||
s = fun(n) ;
|
||
fprintf(wf, "%d\n", s) ;
|
||
}
|
||
fclose(fp) ;
|
||
fclose(wf) ;
|
||
}
|
||
|
||
main() /*主函数*/
|
||
{ int n;
|
||
system("cls");
|
||
n=1000;
|
||
printf("n=%d,f=%d\n",n,fun(n));
|
||
NONO();
|
||
}
|