题1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int fun_jc(int n)
{
if(n == 1)
return 1;
else
return n*fun_jc(n-1);
}
int main(int argc, const char *argv[])
{
//递归实现n的阶乘
int n;
printf("输入n的值:\n");
scanf("%d",&n);
int num = fun_jc(n);
printf("%d阶乘的结果为:%d\n",n,num);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/31e9c6c153181d133df823fc5a0283bf.png)
题2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int fun_sum(int n)
{
if(n == 1)
return 1;
else
return n+fun_sum(n-1);
}
int main(int argc, const char *argv[])
{
//递归计算1~n的和
int n;
printf("输入n的值:\n");
scanf("%d",&n);
int num = fun_sum(n);
printf("1~%d的和为:%d\n",n,num);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/677bd45d5d9b47ecd99e4dcf43acc496.png)
题3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int num=0;
int fun_fbnq(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return fun_fbnq(n - 1) +fun_fbnq(n - 2);
}
}
void print_fbnq(int n)
{
for(int i=1;i<=n;i++)
printf("%d ", fun_fbnq(i));
puts("");
}
int main(int argc, const char *argv[])
{
//斐波那契
printf("输入斐波那契数列长度:\n");
int n;
scanf("%d",&n);
print_fbnq(n);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/46a09f867f73424d0f5939609d59e936.png)