본문 바로가기
PS/BOJ

[자바] 백준 23810 - 골뱅이 찍기 - 뒤집힌 ㅋ (boj java)

by Nahwasa 2022. 7. 16.

문제 : boj23810

 

  규칙을 찾고 구현해보자!

@가 가득찬 경우를 type 0, 좌측에 일부분만 @가 있는 경우를 type 1이라고 해보자.

type 0의 경우 n행에 걸쳐서 5n개의 골뱅이를 출력한다.

type 1의 경우 n행에 걸쳐서 n개의 골뱅이를 출력한다.

 

type에 따라 위의 규칙에 맞춰 출력해주는 함수를 구현한 후,

type 0;

type 1;

type 0;

type 1;

type 1;

순서대로 출력을 해주면 된다!

 

코드 : github

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

public class Main {
    private void print(int n, StringBuilder sb, int type) {
        for (int i = 0; i < n; i++) {
            switch (type) {
                case 0:
                    for (int j = 0; j < 5 * n; j++) {
                        sb.append('@');
                    }
                    sb.append('\n');
                    break;
                case 1:
                    for (int j = 0; j < n; j++) {
                        sb.append('@');
                    }
                    sb.append('\n');
                    break;
            }
        }
    }
    private void solution() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();
        print(n, sb, 0);
        print(n, sb, 1);
        print(n, sb, 0);
        print(n, sb, 1);
        print(n, sb, 1);
        System.out.println(sb);
    }
    public static void main(String[] args) throws Exception {
        new Main().solution();
    }
}

댓글