酷代码 AI
菜单
服务商

本题要求实现求Fabonacci数列项的函数。Fabonacci数列的定义如下: f(n)=f(n−2)+f(n−1) (n≥2),其中f(0)=0,f(1)=1。 函数接口定义: int f( int n ); 函数f应返回第n个Fabonacci数。题目保证输入输出在长整型范围内。建议用递归实现。 裁判测试程序样例: #include <stdio.h> int f( int n ); int main() { int n; scanf("%d", &n); printf("%d\n", f(n)); return 0; } /* 你的代码将被嵌在这里 */

```c int f( int n ) { // 当n为0时,根据定义返回0 if (n == 0) { return 0; } // 当n为1时,根据定义返回1 if (n == 1) { return 1; } // 当n大于等于2时,递归调用f函数计算第n个Fabonacci数 return f(n - 2) + f(n - 1); } ``` [2025-12-10 23:14:36 | AI问答 | 118点数解答]

相关提问
发财导航,免费问AI
实用工具查看更多