qfedu-c-level/day5/homework/h12.c

29 lines
575 B
C
Raw Permalink Normal View History

2023-07-08 16:16:58 +08:00
// 请编程, 输入一个整数n 输出斐波那契数列中第n位上的数值
// 【提示】斐波那契数列: 1 1 2 3 5 8 13 ...
// 【示例】n = 1, 输出1; n = 7, 输出13.
#include <stdio.h>
int main()
{
int a = 1, b = 1, c, n;
printf("请输入一个整数 n : ");
scanf("%d", &n);
if (n >= 1)
c = a;
if (n >= 2)
c = b;
int temp = n - 2;
while (n > 2 && temp > 0)
{
c = a + b;
// printf("%d ", c);
a = b;
b = c;
temp--;
}
printf("%d\n", c);
return 0;
}