有n步台阶,一次只能上1步或2步,共有多少种走法。#include <stdio.h> int fun ( int n ); int main() { int n; scanf("%d", &n); printf("%d", fun(n)); return 0; }补全该程序
时间: 2024-01-29 07:02:08 浏览: 21
下面是使用递归法实现的补全代码:
```c
#include <stdio.h>
int fun(int n) {
if (n == 1 || n == 2) {
return n;
} else {
return fun(n-1) + fun(n-2);
}
}
int main() {
int n;
scanf("%d", &n);
printf("%d", fun(n));
return 0;
}
```
此程序与上一个问题的程序实现类似,使用递归法实现,时间复杂度为$O(2^n)$,存在大量的重复计算,但是代码比较简洁。
相关问题
#include <stdio.h> int fun(int x)
#include <stdio.h> 是C语言中的一个预处理指令,它告诉编译器在编译过程中要包含stdio.h头文件。stdio.h是标准输入输出库的头文件,其中包含了一些常用的输入输出函数的声明,比如printf和scanf。
int fun(int x) 是一个函数的声明,它定义了一个名为fun的函数,该函数接受一个整数参数x,并返回一个整数值。函数声明告诉编译器函数的名称、参数类型和返回类型,但不包含函数的具体实现。
完成以下C语言代码功能:将主函数中输入的字符串反序存放。#include<stdio. h> #include<conio. h> #include(string. h> #define N 50 void fun(char *str, int n)
我可以提供一个示例代码:#include <stdio.h>
#include <string.h>int main ()
{
char str[100], temp;
int i, j; printf("输入一个字符串: ");
gets(str); j = strlen(str) - 1; for (i = 0; i < j; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
} printf("反序后的字符串: %s", str); return 0;
}
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)