코딩하는 개굴이

BaekJoon/BOJ [JAVA] 부녀회장이 될테야_2775 본문

알고리즘/Baekjoon

BaekJoon/BOJ [JAVA] 부녀회장이 될테야_2775

개굴이모자 2019. 2. 2. 21:53
반응형

BaekJoon/BOJ [JAVA] 부녀회장이 될테야_2775

HINT: 왜 이 문제의 제목이 부녀회장이 될테야일까? 부녀회장과는 대체 무슨상관인걸까...? 부녀회장이 각 거주자 수를 통계를 내어야한다 이런내용으로 부녀회장은 힘들어 이게 더 맞지 않았을까....(끄적끄적)
재귀로 더해 나가도록 하였다. 규칙을 찾으려고 하였지만 이쪽이 더 편하다!


import java.util.Scanner;
public class baekjoon_2775 {
    /*
    * 0층 1 2 3 4 5...
    * 1층 1 3 6 10 15...
    * 2층 1 4 10 20 25...
    * */
    public static void main(String[] argc) {
        Scanner scanner = new Scanner(System.in);
        int iter = Integer.parseInt(scanner.nextLine());
        for(int i = 0; i < iter; i++) {
            int floor = Integer.parseInt(scanner.nextLine());
            int room = Integer.parseInt(scanner.nextLine());
            System.out.println(residents_num(floor, room));
        }
    }
    public static int residents_num(int floor, int room) {
        int residents = 0;
        if(floor == 0) return room;
        else {
            for(int f = 1;f <= room;f++ ){
                residents += residents_num(floor-1,f);
            }
            return residents;
        }
    }
}



반응형
Comments