首页 > 试题广场 >

跳台阶

[编程题]跳台阶
  • 热度指数:14943 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

数据范围:
要求:时间复杂度: ,空间复杂度:

输入描述:
本题输入仅一行,即一个整数 n


输出描述:
输出跳上 n 级台阶有多少种跳法
示例1

输入

2

输出

2

说明

青蛙要跳上两级台阶有两种跳法,分别是:先跳一级,再跳一级或者直接跳两级。因此答案为2        
示例2

输入

7

输出

21
import java.math.BigInteger;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            BigInteger count = BigInteger.valueOf(1);
            for (int i = 0; i < a; i++) {
                if ((a - i) % 2 == 0) {
                    if (i > 0) {
                    //数学的排列组合公式:排列总个数的阶乘/每个元素个数阶乘的乘积
                        count = count.add(jc(i + (a - i) / 2).divide(jc(i).multiply(jc((a - i) / 2))));
                    } else {
                        count = count.add(BigInteger.valueOf(1));
                    }

                }
            }
            System.out.println(count);
        }
    }

    /**阶乘
     */
    private static BigInteger jc(int num) {
        BigInteger count = BigInteger.valueOf(num);
        for (int i = num - 1; i > 1; i-- ) {
            count = count.multiply(BigInteger.valueOf(i));
        }
        return count;
    }
}

编辑于 2024-04-08 14:27:43 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            /*int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a + b);*/
            int num = in.nextInt();
            int arr[] = new int[num + 1];
            if(num == 1 || num == 2){
                System.out.println(num);
                return;
            }
            arr[1] = 1;
            arr[2] = 2;
            for(int i = 3;i <= num;i++){
                arr[i] = arr[i - 2] + arr[i - 1];
            }
            System.out.println(arr[num]);
        }
    }
}

发表于 2022-09-23 11:19:21 回复(0)
import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        int res = helper(n);
        System.out.println(res);
    }
    
    public static int helper(int n) {
        // 跳一级有一种跳法,跳两级有两种跳法
        if(n < 3)    return n;
        int first = 1, second = 2;
        for(int i = 3; i <= n; i++) {
            int tmp = first + second;
            first = second;
            second = tmp;
        }
        return second;
    }
}

发表于 2022-07-21 11:44:08 回复(0)
import java.util.Scanner;
public class Main{
    public static int func(int n){
        if(n==1)
            return 1;
        if(n==2)
            return 2;
        return func(n-1)+func(n-2);
    }
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        int ret=func(n);
        System.out.println(ret);
    }
}
发表于 2022-06-23 15:49:10 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] arg){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        if (n<2) System.out.println(1);
        else{
            int[][] arr = mat(n-1);
            System.out.println(arr[0][0]+arr[0][1]);
        }
    }
    
    public static int[][] mat(int n){
        int[][] arr = new int[2][2];
        if (n==1){
            arr[0][0]  = arr[0][1] = arr[1][0] = 1;  arr[1][1] = 0;
        }
        else {
            if ((n&1)==1) {
                int[][] temp = mat(n-1);
                arr[0][0] = temp[0][0]+temp[1][0];
                arr[0][1] = temp[0][1]+temp[1][1];
                arr[1][0] = temp[0][0];
                arr[1][1] = temp[0][1];
            }
            else {
                int[][] temp = mat(n>>1);
                arr[0][0] = temp[0][0]*temp[0][0]+temp[0][1]*temp[1][0];
                arr[0][1] = temp[0][0]*temp[0][1]+temp[0][1]*temp[1][1];
                arr[1][0] = temp[1][0]*temp[0][0]+temp[1][1]*temp[1][0];
                arr[1][1] = temp[1][0]*temp[0][1]+temp[1][1]*temp[1][1];
            }
        }
        return arr;
    }
}
发表于 2022-04-04 21:16:27 回复(0)