题解 | #走方格的方案数#
走方格的方案数
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); } } }