题解 | #走方格的方案数#
走方格的方案数
http://www.nowcoder.com/practice/e2a22f0305eb4f2f9846e7d644dba09b
//数学方法,相当于一共要走(n+m)步,其中往右走要走n步,问题是在(n+m)步里哪些步是
//往右走。相当于在(n+m)个中取m个有多少种组合的问题。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int m = sc.nextInt();
System.out.println(combination(n, n+m));
}
sc.close();
}
public static int combination(int m, int n){//排列C(m,n)
int above = factorial(n);
int below = factorial(n-m) * factorial(m);
return above / below;
}
public static int factorial(int n){//阶乘
if(n>1){
return n*factorial(n-1);
}
return n;
}
}