题解 | #走方格的方案数#

走方格的方案数

https://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static int count = 0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int m = in.nextInt();
            countPaths(0,0, n, m);
            System.out.println(count);
        }
    }
    private static void countPaths(int i, int j, int n, int m){
        if(i > n || j  > m){
            return;
        }

        //到达终点
        if(i == n && j == m){
            count++;
            return;
        }

        //向右走
        countPaths(i, j + 1, n, m);

        //向下走
        countPaths(i + 1, j , n, m);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务