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

走方格的方案数

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

import java.util.*;

/**
 *
 */
public class Main {
    static int n;
    static int m;
    static int total;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            total = 0;
            n = sc.nextInt();
            m = sc.nextInt();
            move(0, 0);
            System.out.println(total);
        }
    }

    private static void move(int x, int y) {
        int[] arr = {0, 1}; // 分为向右和向下两种情况。0表示向右,1表示向下

        for (int j : arr) {
            int nextX = x;
            int nextY = y;
            if (j == 0) { // 判断是否可以往右走
                if (x < n) {
                    nextX++;
                } else {
                    continue;
                }
            } else { // 判断是否可以往下走
                if (y < m) {
                    nextY++;
                } else {
                    continue;
                }
            }
            if (nextX == n && nextY == m) { // 到达终点
                total++;
                return;
            }
            move(nextX, nextY);
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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