题解 | #走方格的方案数#
走方格的方案数
https://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String a;
try {
a = in.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
char[] charAy = a.toCharArray();
int i = 0, l = charAy.length, val = 0, n = 0, m = 0;
while (i < l) {
if (charAy[i] == ' ') {
n = val;
val = 0;
} else {
val += charAy[i] - '0';
if (i == l - 1) m = val;
}
i++;
}
System.out.print(compute(n + 1, m + 1));
}
public static int compute(int n, int m) {
int[][] dp = new int[n][m];
dp[0][0] = 1;
int i = 1, j = 1;
while (i < n) {
dp[i][0] = 1;
i++;
}
while (j < m) {
dp[0][j] = 1;
j++;
}
i = 1;
while (i < n) {
j = 1;
while (j < m) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
j++;
}
i++;
}
return dp[n - 1][m - 1];
}
}
查看16道真题和解析