본문 바로가기
알고리즘/문제풀이

[JAVA]백준 - 2920.음계

by 겅아링 2020. 11. 4.
반응형

www.acmicpc.net/problem/2920

 

2920번: 음계

다장조는 c d e f g a b C, 총 8개 음으로 이루어져있다. 이 문제에서 8개 음은 다음과 같이 숫자로 바꾸어 표현한다. c는 1로, d는 2로, ..., C를 8로 바꾼다. 1부터 8까지 차례대로 연주한다면 ascending, 8

www.acmicpc.net

 

첫 풀이>

한 숫자씩 비교해서 전부 맞는지 확인했다
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Baekjoon2920 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int start = Integer.parseInt(st.nextToken());
        int ascending = 0;
        int descending = 0;
        if (start == 1) {
            for (int i = 2; i <= 8; i++) {
                if (Integer.parseInt(st.nextToken()) == i) ascending++;
            }
        } else if (start == 8) {
            for (int i = 7; i >= 1; i--) {
                if (Integer.parseInt(st.nextToken()) == i) descending++;
            }
        }

        if (ascending == 7) System.out.print("ascending");
        else if (descending == 7) System.out.print("descending");
        else System.out.print("mixed");
        br.close();
    }
}

 

다른 풀이 >

1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
전부 맞는지만 확인하면 되니 통째로 같은지 검사
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Baekjoon2920 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        if (str.equals("1 2 3 4 5 6 7 8")) System.out.print("ascending");
        else if (str.equals("8 7 6 5 4 3 2 1")) System.out.print("descending");
        else System.out.print("mixed");
        br.close();
    }
}

 

반응형

'알고리즘 > 문제풀이' 카테고리의 다른 글

[JAVA]백준 - 2338.긴자리 계산  (0) 2020.11.04
[JAVA]백준 - 1271.엄청난 부자2  (0) 2020.11.04
[JAVA]백준 - 1924.2007년  (0) 2020.10.27
[JAVA]백준 - 2588.곱셈  (0) 2020.10.25
[JAVA]백준 - 1541.잃어버린 괄호  (0) 2020.09.14