์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step8] 2775 : ๋ถ€๋…€ํšŒ์žฅ์ด ๋ ํ…Œ์•ผ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 17. 15:01

www.acmicpc.net/problem/2775

 

2775๋ฒˆ: ๋ถ€๋…€ํšŒ์žฅ์ด ๋ ํ…Œ์•ผ

์ฒซ ๋ฒˆ์งธ ์ค„์— Test case์˜ ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ์˜ ์ผ€์ด์Šค๋งˆ๋‹ค ์ž…๋ ฅ์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ์ค„์— ์ •์ˆ˜ k, ๋‘ ๋ฒˆ์งธ ์ค„์— ์ •์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค

www.acmicpc.net


๋ฌธ์ œ

ํ‰์†Œ ๋ฐ˜์ƒํšŒ์— ์ฐธ์„ํ•˜๋Š” ๊ฒƒ์„ ์ข‹์•„ํ•˜๋Š” ์ฃผํฌ๋Š” ์ด๋ฒˆ ๊ธฐํšŒ์— ๋ถ€๋…€ํšŒ์žฅ์ด ๋˜๊ณ  ์‹ถ์–ด ๊ฐ ์ธต์˜ ์‚ฌ๋žŒ๋“ค์„ ๋ถˆ๋Ÿฌ ๋ชจ์•„ ๋ฐ˜์ƒํšŒ๋ฅผ ์ฃผ์ตœํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

์ด ์•„ํŒŒํŠธ์— ๊ฑฐ์ฃผ๋ฅผ ํ•˜๋ ค๋ฉด ์กฐ๊ฑด์ด ์žˆ๋Š”๋ฐ, “a์ธต์˜ bํ˜ธ์— ์‚ด๋ ค๋ฉด ์ž์‹ ์˜ ์•„๋ž˜(a-1)์ธต์˜ 1ํ˜ธ๋ถ€ํ„ฐ bํ˜ธ๊นŒ์ง€ ์‚ฌ๋žŒ๋“ค์˜ ์ˆ˜์˜ ํ•ฉ๋งŒํผ ์‚ฌ๋žŒ๋“ค์„ ๋ฐ๋ ค์™€ ์‚ด์•„์•ผ ํ•œ๋‹ค” ๋Š” ๊ณ„์•ฝ ์กฐํ•ญ์„ ๊ผญ ์ง€ํ‚ค๊ณ  ๋“ค์–ด์™€์•ผ ํ•œ๋‹ค.

์•„ํŒŒํŠธ์— ๋น„์–ด์žˆ๋Š” ์ง‘์€ ์—†๊ณ  ๋ชจ๋“  ๊ฑฐ์ฃผ๋ฏผ๋“ค์ด ์ด ๊ณ„์•ฝ ์กฐ๊ฑด์„ ์ง€ํ‚ค๊ณ  ์™”๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์„ ๋•Œ, ์ฃผ์–ด์ง€๋Š” ์–‘์˜ ์ •์ˆ˜ k์™€ n์— ๋Œ€ํ•ด k์ธต์— nํ˜ธ์—๋Š” ๋ช‡ ๋ช…์ด ์‚ด๊ณ  ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•˜๋ผ. ๋‹จ, ์•„ํŒŒํŠธ์—๋Š” 0์ธต๋ถ€ํ„ฐ ์žˆ๊ณ  ๊ฐ์ธต์—๋Š” 1ํ˜ธ๋ถ€ํ„ฐ ์žˆ์œผ๋ฉฐ, 0์ธต์˜ iํ˜ธ์—๋Š” i๋ช…์ด ์‚ฐ๋‹ค.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— Test case์˜ ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ์˜ ์ผ€์ด์Šค๋งˆ๋‹ค ์ž…๋ ฅ์œผ๋กœ ์ฒซ ๋ฒˆ์งธ ์ค„์— ์ •์ˆ˜ k, ๋‘ ๋ฒˆ์งธ ์ค„์— ์ •์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค

 

์ถœ๋ ฅ

๊ฐ๊ฐ์˜ Test case์— ๋Œ€ํ•ด์„œ ํ•ด๋‹น ์ง‘์— ๊ฑฐ์ฃผ๋ฏผ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋ผ.

 

์ œํ•œ

1 ≤ k, n ≤ 14

 

์˜ˆ์ œ ์ž…๋ ฅ

2

1

3

2

3

 

์˜ˆ์ œ ์ถœ๋ ฅ

6

10


ํ’€์ด

1. ์ตœ๋Œ€ ๋ฒ”์œ„๊ฐ€ k,n ๋ชจ๋‘ 14์ด๋ฏ€๋กœ ๋ฏธ๋ฆฌ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๊ฐ’์„ ๋„ฃ์–ด๋‘”๋‹ค.

 

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class B2775 {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int t = sc.nextInt();
		int[][] array = new int[15][15];
		
		for(int i = 1; i < 15; i++) {
			array[i][1] = 1;
			array[0][i] = i;
		}
		
		for(int i = 1; i < 15; i++) {
			for(int j = 1; j < 15; j++) {
				array[i][j] = array[i-1][j] + array[i][j-1];
			}
		}
		
		for(int i = 0; i < t; i++) {
			int k = sc.nextInt();
			int n = sc.nextInt();
			
			bw.write(array[k][n] + "\n");
		}
		
		bw.flush();
	}
}