首页 > 技术文章 > 习题6-4 使用函数输出指定范围内的Fibonacci数

2018jason 2019-06-03 09:30 原文

 1 #include<stdio.h>
 2 
 3 int fib(int n);
 4 void PrintFN(int m, int n);
 5 int main(void)
 6 {
 7     int m, n, t;
 8 
 9     scanf_s("%d %d %d", &m, &n, &t);
10     printf("fib(%d) = %d\n", t, fib(t));
11     PrintFN(m, n);
12 
13     return 0;
14 }
15 
16 int fib(int n)
17 {
18     int a, b, t;
19     a = 0;
20     b = 1;
21     /*
22         斐波拉契数列
23     */
24     for (int i = 2; i <= n; i++)
25     {
26         t = a + b;
27         a = b;
28         b = t;
29     }
30 
31     return b;
32 }
33 void PrintFN(int m, int n)
34 {
35     int i = 1;
36     int count = 0;            //对符合条件的斐波拉契数进行统计
37     while (fib(i) <= n)
38     {
39         if (fib(i) >= m)
40         {
41             count++;
42             if (count == 1)
43             {
44                 printf("%d", fib(i));
45             }
46             else
47             {
48                 printf(" %d", fib(i));
49             }
50         }
51         i++;
52     }
53     if (count == 0)
54     {
55         printf("No Fibonacci number\n");
56     }
57 }

 

推荐阅读