알고리즘 문제풀이/Beakjoon

백준 5356. Triangles (Java)

joah.k 2023. 7. 22. 22:15
728x90

https://www.acmicpc.net/problem/5356

 

5356번: Triangles

Read in a letter and a number. The number indicates how big the letter triangle should be. The number indicating the size of the triangle will have a range from 0 to 250 (i.e., num>=0 and num<=250). The letters must wrap around from Z to A. If you start wi

www.acmicpc.net

 

 

Triangles 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 236 176 159 79.500%

문제

Read in a letter and a number. The number indicates how big the letter triangle should be. The number indicating the size of the triangle will have a range from 0 to 250 (i.e., num>=0 and num<=250).

The letters must wrap around from Z to A. If you start with Z and have to print 5 levels, you must wrap around and start with A after the Z level is complete.

입력

The first number indicates the number of data sets to follow. Each data set will contain one letter and one number. All letter input will be uppercase.

출력

Print out the letter triangles in the order given. There is one blank line between each letter triangle.

예제 입력 1 복사

3
5 A
3 Z
4 C

예제 출력 1 복사

A
BB
CCC
DDDD
EEEEE

Z
AA
BBB

C
DD
EEE
FFFF
 

풀이 :

알파벳과 숫자를 입력받아, 주어진 알파벳부터 시작해서 삼각형 모양으로 줄마다 1개씩 증가하는 개수만큼 알파벳을 차례대로 출력하고, 'Z' 다음에는 'A'부터 다시 시작하는 문제. 

삼각형 모양 반복문이야 익숙하고, 이 문제에서 생각할 것은 2 가지. 

1) 주어진 알파벳으로부터 이어지는 알파벳 구하기 : A->B->C... 
2) 알파벳의 시작과 끝 처리 : A~Z의 반복, Z 다음엔 A가 나와야 함

 

삼각형 형태를 만드는 반복문의 형태는 다음과 같다. 

public class Beakjoon5356 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());

        for(int i=0; i<t; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int n = Integer.parseInt(st.nextToken());
            char alphabet = st.nextToken().charAt(0); // 시작 문자열

            for (int j = 0; j < n; j++) {
                for (int k = 0; k <= j; k++) 
                
                    // 알파벳 출력 부분 
                    
                    System.out.print((char) ((alphabet - 'A' + j) % 26 + 'A'));
                }
                System.out.println();
            }
            // Print a blank line between letter triangles
            if (i < t - 1) {
                System.out.println();
            }
        }
    }
}
 
 

이제 반복에 맞게 알파벳을 출력하는 부분을 작성해야 하는데 단계별로 풀어보자면... 

1. 알파벳 처리 : 입력받은 문자 alphabet 에서 'A' 를 뺀다.
자바에서는 내부적으로 숫자 값으로 저장되므로 이렇게 하면 'A'로 부터 몇 번째 알파벳인지 알 수 있다.

  ex) 'B'-'A'=1, 'C'-'A'=2 

 

2. alphabet - 'A' + j : 입력받은 문자열로부터 ~ 증가하는 변수인 j를 더한다. 

  ex) B 부터 시작,  j 가 2 까지일 때  B로부터 2개 증가한 알파벳을 구함 

 

3. Z->A 설정 : 알파벳은 총 26자이다. (alphabet - 'A' + j)%26 을 해서 A부터 Z 까지만 반복되도록 설정 

4.   ( (alphabet - 'A' + j)%26 ) + 'A' : 실제 출력할 대문자 알파벳을 구함

  ex)  예를 들어 j=1, 입력값 alphabet='A'이라 가정 한다면 (alphabet - 'A' + j)%26 )의 계산 결과는 1 이 된다.
        이 값에 아스키 코드 65인 'A'를 더하면 새로운 문자 'B'(아스키 코드 66) 이 된다.

5. 그리고 형변환을 통해 숫자로 입력된 알파벳을 문자로 변환 

 

 

728x90