C/C++经典程序训练2---斐波那契数列
Time Limit: 1000MS Memory Limit: 65536KBProblem Description
编写计算斐波那契(Fibonacci)数列的第n项函数fib(n)(n<40)。 数列: f1=f2==1; fn=fn-1+fn-2(n>=3)。Input
输入整数n的值。Output
输出fib(n)的值。Example Input
7Example Output
13Hint
Author
#include<stdio.h>
02
int
f(
int
n)
03
{
04
int
y;
05
if
(n == 1 || n == 2)
06
{
07
y = 1;
08
}
09
else
10
{
11
y = f(n - 1) + f(n - 2);
12
}
13
return
y;
14
}
15
int
main()
16
{
17
int
n, y;
18
scanf
(
"%d"
, &n);
19
y = f(n);
20
printf
(
"%d\n"
, y);
21
return
0;
22
}
23
24
C/C++经典程序训练2---斐波那契数列
最新推荐文章于 2023-09-19 18:47:40 发布