Contents

[백준 2775]부녀회장이 될테야 - JAVA 풀이

   Aug 21, 2021     2 min read     - Comments

바로가기

문제

문제


해석

  1. 0 ~ 14 층, 1 ~ 14호 까지 이루어진 아파트이다.

  2. 0층에는 호와 같은 수의 사람이 살고 있다.

  3. a층 b호에 사는 사람의 인원 수는 (a-1)층의 1 ~ b호 까지의 인원 수의 합이다.


풀이

  1. 미리 계산을 다 해놓고 데이터를 가져오는 방식으로 풀었다.

  2. 15 * 15의 2차원 배열을 생성해준다. 0부터 시작하므로 15개가 필요하며, 호수는 계산하기 쉽게 15개로 만들고 0번째는 비워둔다.

  3. 0층은 호수와 같은 인원으로 입력한다.(1호는 1명, 2호는 2명 … 14호는 14명)

  4. 층별로 1호에는 1명씩 산다.

  5. 그 외에 인원수는 아래 표와 같다.

그림1

a층 b호 인원수 = a층 b-1호 + a-1층 b호


코드

import java.io.*;

public class Main
{  
    public static void main(String args[]) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int t = Integer.parseInt(br.readLine());
        int[][] apt = calc();        
        
        for(int i = 0 ; i < t; i++){
            int k = Integer.parseInt(br.readLine());
            int n = Integer.parseInt(br.readLine());
            bw.write(apt[k][n] + "\n");
        }        
        bw.flush();
        bw.close();
    }    
    public static int[][] calc(){
        int[][] arr = new int[15][15];
        
        for(int i = 1; i < 15; i++){
            arr[0][i] = i;  //0층 1 ~ 14호 인원
            arr[i][1] = 1;  //1 ~ 14층 1호의 인원
        }        
        for(int i = 1; i < 15; i++)
            for(int j = 2; j < 15; j++)
                arr[i][j] = arr[i-1][j] + arr[i][j-1];                
      
        return  arr;
    }
}