浙大版《C语言程序设计(第3版)》题目集 习题6-4 使用函数输出指定范围内的Fibonacci数 (20分)
程序员文章站
2022-06-08 17:58:33
...
#include <stdio.h>
int fib(int n);
void PrintFN(int m, int n);
int main()
{
int m, n, t;
scanf("%d %d %d", &m, &n, &t);
printf("fib(%d) = %d\n", t, fib(t));
PrintFN(m, n);
return 0;
}
int fib(int n)
{
int a = 1, b = 1, c, i;
for (i = 1; i < n; i++)
{
c = a + b;
a = b;
b = c;
}
return a;
}
void PrintFN(int m, int n)
{
int i, flag = 0, t = 1;
for (i = 1; fib(i) <= n; i++)
{
if (fib(i) >= m)
{
if (t)
{
printf("%d", fib(i));
t = 0;
flag = 1;
}
else
{
printf(" %d", fib(i));
flag = 1;
}
}
}
if (flag == 0)
printf("No Fibonacci number");
}