본문 바로가기
Problem Solving/Baekjoon

[백준] 3578 Holes - Greedy / Java

by graycode 2024. 4. 10.

 문제 링크

 

3578번: Holes

You may have seen a mechanic typewriter — such devices were widespread just 15 years ago, before computers replaced them. It is a very simple thing. You strike a key on the typewriter keyboard, the corresponding type bar rises, and the metallic letter mo

www.acmicpc.net

 

 풀이 코드

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

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int h = read();

        if (h == 0) sb.append("1");
        else if (h == 1) sb.append("0");
        else {
            if ((h & 1) == 1) sb.append("4");
            int i = h / 2;
            while (i-- > 0) sb.append("8");
        }

        bw.write(sb.toString());
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글