题解 | #走方格的方案数#
走方格的方案数
https://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
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();
System.out.println(med(n,m));
}
}
public static int med(int n,int m){
if(n==1||m==1){ //递归结束条件
return m+n;
}
else return med(n-1,m)+med(n,m-1);//没到结束,继续递归
}
}
查看23道真题和解析
快手成长空间 763人发布