Baekjoon - 11050

Tadap·2023년 9월 5일
0

Baekjoon

목록 보기
7/94
post-custom-banner

문제

Solved.ac Class 2+

1차시도

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] split = br.readLine().split(" ");
		int n = Integer.parseInt(split[0]);
		int k = Integer.parseInt(split[1]);

		System.out.println(factorial(n) / (factorial(k) * factorial(n - k)));
	}

	private static long factorial(int n) {
		if (n <= 1) {
			return n;
		}
		return factorial(n - 1) * n;
	}
}

runtim error (/by zero)

2차시도

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] split = br.readLine().split(" ");
		int n = Integer.parseInt(split[0]);
		int k = Integer.parseInt(split[1]);

		System.out.println(factorial(n) / (factorial(k) * factorial(n - k)));
	}

	private static long factorial(int n) {
		if (n == 0) {
			return 1;
		}
		return factorial(n - 1) * n;
	}
}

if 부분을 바꿔 해결

post-custom-banner

0개의 댓글