C language Fibonacci sequence

Function is: t is greater than a small number of seek by the Fibonacci sequence, the result returned by the function. Wherein the Fibonacci sequence F (n) is defined as:
F. (0) = 0, F. (. 1) =. 1
F (n) = F. (N--. 1) + F. (N--2)
last call function writeDat () read data 50 t, and the results obtained are output to a file in out.dat.
For example: when t = 1000, the value of the function: 1597.
#include <stdio.h>

int jsValue(int t)

{Int f0 = 0, f1 = 1, fn;

fn = f0 + f1;

while(fn<=t)

{F 0 = f 1;

f1 = fn; fn = f0 + f1;

}

return fn;

}

main()

{

int n;

=1000;

printf(“n=%d,f=%d\n”,n,jsValue(n)); writeDat();

}

writeDat()

{

FILE *in,*out;

int i,n,s;

in=fopen(“in.dat”,“r”);

out=fopen(“out.dat”,“w”);

for(i=0;i<50;i++){

fscanf(in,"%d",&n);

s=jsValue(n);

printf("%d\n",s);

fprintf(out,"%d\n",s);

}

fclose(in);

fclose(out);

}

Published 239 original articles · won praise 3 · Views 3153

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105176781