C语言实现斐波那契数列:代码解析与优化
你的代码有一些问题。首先,你在函数'shulie()'中没有给变量'i'和'ch'指定类型。其次,在递归调用'shulie()'时,你没有传入参数。最后,你在'scanf()'中使用了'%c'来读取整数,应该使用'%d'。
这是修改后的代码:
#include <stdio.h>
void shulie(int p)
{
static int i = 2;
int ch = 0;
if (i < p)
{
printf('%d ', ch = (ch - 1) + (ch - 2));
i++;
shulie(p);
}
else
{
return;
}
}
int main()
{
int p;
printf('请输入一个整数: ');
scanf('%d', &p);
shulie(p);
return 0;
}
这个代码将打印出斐波那契数列的前'p'个数字。
原文地址: https://www.cveoy.top/t/topic/pEvQ 著作权归作者所有。请勿转载和采集!