题解 | #走方格的方案数#--二维数组
走方格的方案数
https://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b
import java.io.*;
public class Main {
public static void main(String [] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = br.readLine()) != null) {
String[] nums = str.split(" ");
int m = Integer.parseInt(nums[1]);
int n = Integer.parseInt(nums[0]);
getPath(m, n);
}
}
public static void getPath(int m, int n) {
int[][] map = new int[m + 1][n + 1];
map[0][0] = 0;
// 当横竖都是一的时候,只有一条路线
for (int i = 1; i <= m; i++) {
map[i][0] = 1;
}
for (int i = 1; i <= n; i++) {
map[0][i] = 1;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
map[i][j] = map[i - 1][j] + map[i][j - 1];
}
}
System.out.println(map[m][n]);
}
}
查看24道真题和解析