(구) solved ac class 문제들/class 1

[class 1]백준 음계(2920번) 풀이 (C++/Python)

isekaipudding 2026. 4. 24. 12:01

문제 : 음계(2920번)(Bronze II)

문제 링크 : https://www.acmicpc.net/problem/2920
출처 : Baekjoon Online Judge

 

1,2,3,4,5,6,7,8 순서로 연주하면 ascending, 8,7,6,5,4,3,2,1 순서로 연주하면 descending, 둘 다 아니라면 mixed로 출력된다고 합니다.

그런데 문제를 잘 읽어보면 "1부터 8까지 숫자가 한 번씩 등장한다."가 있습니다.

즉, ascending으로 나오는 경우는 12345678이 유일하며 descending으로 나오는 경우는 87654321가 유일하고 나머지는 모두 mixed입니다.

이것을 C++로 구현하면 다음과 같이 나옵니다.

#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    char ch[8];
    for(int i = 0; i < 8; i++) {
        char c;
        cin >> c;
        ch[i] = c;
    }

    string s = "";
    for(int i = 0; i < 8; i++) s += ch[i];

    if(s == "12345678") cout << "ascending";
    else if(s == "87654321") cout << "descending";
    else cout << "mixed";

    return 0;
}

"1부터 8까지 숫자가 한 번씩 등장한다." 조건 덕분에 문제를 좀 더 쉽게 해결할 수 있었습니다.

 

Python은 이것보다 더 쉽게 구현할 수 있습니다.

import sys

input = sys.stdin.readline

S:str = input().rstrip()

if S == "1 2 3 4 5 6 7 8" :
    print("ascending")
elif S == "8 7 6 5 4 3 2 1" :
    print("descending")
else :
    print("mixed")