首页 > 其他分享 >5-18打卡

5-18打卡

时间:2023-05-18 21:02:07浏览次数:37  
标签:index 台阶 int 18 steps printf print 打卡

递归写爬楼梯

#include <stdio.h>

// 定义一个函数,用来打印每次爬的台阶数
void print_steps(int steps[], int n) {
    printf("一种可能的方法是:");
    for (int i = 0; i < n; i++) {
        printf("%d ", steps[i]);
    }
    printf("\n");
}

// 定义一个递归函数,用来求解所有可能的方法
void climb_stairs(int n, int steps[], int index) {
    // 如果只有一个或两个台阶,直接打印结果
    if (n == 1) {
        steps[index] = 1;
        print_steps(steps, index + 1);
    } else if (n == 2) {
        steps[index] = 1;
        print_steps(steps, index + 1);
        steps[index] = 2;
        print_steps(steps, index + 1);
    } else {
        // 如果有多于两个台阶,可以先爬一个或两个台阶,然后递归求解剩下的台阶
        steps[index] = 1;
        climb_stairs(n - 1, steps, index + 1);
        steps[index] = 2;
        climb_stairs(n - 2, steps, index + 1);
    }
}

int main() {
    int n; // 输入台阶数
    printf("请输入台阶数:");
    scanf("%d", &n);
    int steps[n]; // 定义一个数组,用来存储每次爬的台阶数
    climb_stairs(n, steps, 0); // 调用递归函数,求解所有可能的方法
    return 0;
}


标签:index,台阶,int,18,steps,printf,print,打卡
From: https://www.cnblogs.com/wlxdaydayup/p/17413279.html

相关文章

  • 每日打卡
    把真分数分解为埃及分数问题描述:古埃及人用的分数都是分子为一的分数,将真分数拆分成埃及分数问题分析:1如果分子为1直接输出2.分母是分子的倍数,化简后输出3.如果不能消去的话可以分出一个a/b+1和c出来代码:#include<stdio.h>intmain(){ longinta,b,c; printf("请输入a......
  • 5.18
    #include<iostream>usingnamespacestd;#include<string>classstudent{public:   voidshangke();protected:   stringname;   intbj;   intid;};classteacher{public:   voidjiaoke();protected:   intID;   intgz;};c......
  • 5.18总结
    packagecom.mf.jdbc.exmaple;importcom.alibaba.druid.pool.DruidDataSourceFactory;importcom.mf.jdbc.Brand;importorg.junit.Test;importjavax.sql.DataSource;importjava.io.FileInputStream;importjava.sql.Connection;importjava.sql.PreparedStatement;......
  • 每日总结2023-05-18
    今天对项目进行美化对于登录按钮,使用<?xmlversion="1.0"encoding="utf-8"?><ripplexmlns:android="http://schemas.android.com/apk/res/android"android:color="@color/mi_bai"><itemandroid:id="@+id/maske......
  • 2023 5 18
    #include<iostream>#include<fstream>usingnamespacestd;classTercher{public:stringname="";intID=0;charsex='m';};stringname;intID;charsex;intmain(){ofstreamofs("D:\\Visua......
  • c++打卡练习(33)
    求一个真分数的埃及分数表示埃及分数是指只使用1作为分子的分数,例如8/11=1/2+1/5+1/55+1/110;流程图:伪代码:源代码:#include<iostream>usingnamespacestd;intmain(){ inta,b,c,i,j; cout<<"请输入一个真分数"<<endl; cin>>a; getchar(); cin>>b; if(a>b){ cout<<......
  • 5.18CSDN贪吃蛇
    贪吃蛇 速度不要调很慢会影响判断#include<iostream>#include<windows.h>#include<conio.h>#include<deque>#include<ctime>#include<stdexcept>usingnamespacestd;structSnake{//蛇类结构体charimage;shortx,y;//坐标};classsnakeGame......
  • 第二十三天打卡
    一、问题描述C语言实现两个不同的自然数A和B,如果整数A的全部因子(包括1,不包括A本身)之和等于B;且整数B的全部因子(包括1,不包括B本身)之和等于A,则将整数A和B称为亲密数。求3000以内的全部亲密数。二、设计思路1、a和b都是3000以内2、穷举a在3000以内(或穷举b在3000以内)3、通过......
  • 2023.5.18
    importosimportpandasaspd#添加测试数据os.makedirs(os.path.join('.','data'),exist_ok=True)data_file=os.path.join('.','data','house_tiny.csv')withopen(data_file,'w')asf:   f.write('N......
  • 打卡 c语言趣味编程 求最小公倍数
    问题描述:求任意两个正整数的最小公倍数(LCM)。思路:输入两个正整数,假设为num1和num2。定义一个变量lcm并初始化为较大的那个数(即lcm=max(num1,num2))。进入一个循环,循环条件为lcm不能同时被num1和num2整除。在每次循环中,将lcm增加1。循环结束后,lcm的值就是最小......