CPP/计算机组成原理实验代码/Fibonacci.cpp
2023-05-12 00:34:15 +08:00

70 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*------------------------------------------------
【程序设计】
--------------------------------------------------
/* 编写函数fun它的功能是求Fibonacci数列中大于s的最小的一个数结果由函数返回。其中Fibonacci数列F(n)的定义为:
F(0)=0F(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();
}