๊ธฐ๋ก๋ฐฉ

BOJ_11050 : ์ดํ•ญ ๊ณ„์ˆ˜ 1 ๋ณธ๋ฌธ

CodingTest/Java

BOJ_11050 : ์ดํ•ญ ๊ณ„์ˆ˜ 1

Soom_1n 2022. 7. 30. 00:47

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

11050๋ฒˆ: ์ดํ•ญ ๊ณ„์ˆ˜ 1

์ฒซ์งธ ์ค„์— \(N\)๊ณผ \(K\)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ \(N\) ≤ 10, 0 ≤ \(K\) ≤ \(N\))

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ดํ•ญ ๊ณ„์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int answer = 0;
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int K = sc.nextInt();

        int sum = 1;
        for(int i = 1; i <= N; i++) sum *= i;	// N!
        answer = sum;
        sum = 1;

        for(int i = 1 ; i <= K; i++) sum *= i;	// K!

        for(int i = 1 ; i <= N-K; i++) sum *= i;// (N-K)!
        answer /= sum;

        System.out.println(answer);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ดํ•ญ ๊ณ„์ˆ˜์˜ ๊ณ„์‚ฐ ๊ณต์‹ N! / (K!(N-K)!) ๋ฅผ ์ ์šฉํ–ˆ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ดํ•ญ ๊ณ„์ˆ˜์˜ ๊ฐœ๋…์„ ๋ชฐ๋ผ ๊ฒ€์ƒ‰ํ–ˆ๋‹ค. ์กฐํ•ฉ์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ N๊ฐœ์—์„œ R๊ฐœ๋ฅผ ๋ฝ‘๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜์ด๋‹ค. ์ฐธ๊ณ 
  • ๋ฐฑ์ค€ java๋ฌธ์ œ๋Š” ์˜ค๋žœ๋งŒ์ด๋ผ ์ปดํŒŒ์ผ ์—๋Ÿฌ์™€ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ฐธ๊ณ 
    • ์ปดํŒŒ์ผ ์—๋Ÿฌ : class ์ด๋ฆ„์€ ํ•ญ์ƒ Main์œผ๋กœ ์ง€์ •
    • ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ : ํŒจํ‚ค์ง€ ์ด๋ฆ„ ๋นผ์ฃผ๊ธฐ 

728x90