문제 : 음계(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")'(구) solved ac class 문제들 > class 1' 카테고리의 다른 글
| [class 1]백준 OX퀴즈(8958번) 풀이 (C++/Python) (0) | 2026.04.28 |
|---|---|
| [class 1]백준 나머지(3052번) 풀이 (C++/Python) (0) | 2026.04.27 |
| [class 1]백준 문자열 반복(2675번) 풀이 (C++/Python) (0) | 2026.04.23 |
| [class 1]백준 숫자의 개수(2577번) 풀이 (C++/Python) (1) | 2026.04.22 |
| [class 1]백준 단어의 개수(1152번) 풀이 (C++/Python) (0) | 2026.04.21 |