728x90
์กฐํฉ ๊ณต์ nCr์ ์ฌ์ฉํด์ ๋ฌธ์ ๋ฅผ ํ๋ฉด๋๋ค. ์ด ๋, ์ฌ๊ท ํจ์๋ฅผ ๋ณต์ตํ๊ธฐ ์ํด factorial ์ Iteration์ด ์๋ ์ฌ๊ท๋ก ๋ง๋ค์๋ค.
ํ์ค์นผ ์ผ๊ฐํ์ ๋ํด ์์ง ์์ธํ ์์ง๋ ๋ชปํ์ง๋ง, ์ด์ฌํ ๊ณต๋ถํด๋ด์ผ๊ฒ ๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try {
StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
System.out.println(factorial(n)/(factorial(k)*factorial(n-k)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int factorial(int a) {
if(a <= 1) return 1;
else return a*factorial(a-1);
}
}
728x90