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

29 lines
575 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 请编程, 输入一个整数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;
}