코딩테스트

[백준] 10171번 고양이 - JAVA

두두(DoDu) 2023. 7. 19. 20:46
반응형

- 문제 

아래 예제와 같이 고양이를 출력하시오.

\    /\
 )  ( ')
(  /  )
 \(__)|

 

- 입력

없음.

 

- 코드

\는 단독으로 출력이 불가한 문자이다. 그래서 백슬래시를 한번 더 붙이면 하나의 백슬래시가 출력될 수 있다.

 

즉, 이스케이스(escape) 문자는 출력하는 문장 안에서 원하는 형식에 맞추어 출력할 수 있도록 줄을 바꾸는 등의 특별한 의미들을 나타내기 위해 사용된다.

아래의 표는 정석 책에서 한번쯤이라도 봤을 내용일 것이다.

\n 개행 / 줄바꿈
\t 스크린 커서를 다음 탭으로 옮긴다.
\r carriage return. 그 줄의 맨 앞으로 커서를 보낸다.
\\ 역슬래시 문자 출력 시 사용한다.
\' 작은 따옴표(single quotation mark) 표현이다.
\" 큰 따옴표(double quotation mark) 표현이다.

 

ⓛ BufferedWriter

import java.io.*;

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

        bw.write("\\    /\\");
        bw.newLine();

        bw.write(" )  ( ')");
        bw.newLine();

        bw.write("(  /  )");
        bw.newLine();

        bw.write(" \\(__)|");
        bw.flush();
        bw.close();
    }
}

 

② StringBuilder

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StringBuilder sb = new StringBuilder();

        sb.append("\\    /\\\n");
        sb.append(" )  ( ')\n");
        sb.append("(  /  )\n");
        sb.append(" \\(__)|\n");

        System.out.println(sb);
    }
}

 

③ StringBuffer

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StringBuilder sb = new StringBuilder();

        sb.append("\\    /\\\n");
        sb.append(" )  ( ')\n");
        sb.append("(  /  )\n");
        sb.append(" \\(__)|\n");

        System.out.println(sb);
    }
}
반응형