c-moodle/homework/11962.c

21 lines
597 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为数列的项数
int fibonacci(int n) {
if (n == 1 || n == 2) { // 前两项特殊处理
return 1;
}
// 递归调用求解
return fibonacci(n - 1) + fibonacci(n - 2);
}
int x;
int main() {
scanf("%d", &x); // 读入项数
if (x >= 1) { // 如果输入的项数合法大于等于1
printf("%d", fibonacci(x)); // 输出对应项数斐波那契数列的值
} else {
printf("ERROR"); // 否则输出ERROR提示无效输入
}
return 0;
}