1 #include <stdio.h> 2 3 int fib(int n); 4 void PrintFN(int m, int n); 5 6 int main() 7 { 8 int m, n, t; 9 10 scanf_s("%d %d %d", &m, &n, &t); 11 printf("fib(%d) = %d\n", t, fib(t)); 12 PrintFN(m, n); 13 14 return 0; 15 } 16 17 /* 你的代码将被嵌在这里 */ 18 19 int fib(int n) //斐波拉契数列 20 { 21 int a, b, c; 22 a = 1; 23 b = 1; 24 if (n == 1) 25 { 26 return 1; 27 } 28 else 29 { 30 if (n == 2) 31 { 32 return 1; 33 } 34 else 35 { 36 for (int i = 3; i <= n; i++) 37 { 38 c = a + b; 39 a = b; 40 b = c; 41 } 42 return b; 43 } 44 } 45 } 46 47 void PrintFN(int m, int n) 48 { 49 int i = 1; 50 int count = 0; 51 while (fib(i) <= n) 52 { 53 if (fib(i) >= m) 54 { 55 count++; 56 if (count == 1) 57 { 58 printf("%d", fib(i)); //第一个数前面没有空格 59 } 60 else 61 { 62 printf(" %d", fib(i)); //后面每个数的前面有空格 63 } 64 } 65 i++; 66 } 67 if (count == 0) 68 { 69 printf("No Fibonacci number\n"); 70 } 71 }
原文:https://www.cnblogs.com/2018jason/p/10965819.html