-
백준 2920번 : 음계 - 문제 풀이 (Python)PS/백준 - 파이썬 2023. 10. 7. 23:19반응형
https://www.acmicpc.net/problem/2920
조금 복잡하게 푼 것 같기도 하다.
풀이 방식
state를 None으로 두고, 계속 올라가는 경우 state가 ascending으로 유지된다.
계속 내려가는 경우 descening으로 유지된다.
하지만 state가 다른 값으로 대입되려고 하는 경우, mixed를 출력하고 반복문을 중단한다.
for문이 다 돌아가는 경우, 최종 state를 출력한다.
이 때 핵심은 range가 len(notes)-1까지 돌아가야 한다는 것이다.
notes = list(map(int, input().split())) state = None for i in range(len(notes)-1): if notes[i] > notes[i+1]: # 내려가고 있다 if state == "ascending": print("mixed") break state = "descending" elif notes[i] < notes[i+1]: # 올라가고 있다 if state == "descending": print("mixed") break state = "ascending" else: print(state)
반응형'PS > 백준 - 파이썬' 카테고리의 다른 글
백준 10809번 : 알파벳 찾기 - 문제 풀이 (Python) (0) 2023.10.08 백준 2577번 : 숫자의 개수 - 문제 풀이 (Python) (0) 2023.10.08