본문 바로가기
Algorithm/백준 알고리즘 풀이

[Baekjoon] 2920. 음계

by newnu 2021. 4. 15.
반응형

# Problem

다장조는 c d e f g a b C, 총 8개 음으로 이루어져있다. 이 문제에서 8개 음은 다음과 같이 숫자로 바꾸어 표현한다. c는 1로, d는 2로, ..., C를 8로 바꾼다.

1부터 8까지 차례대로 연주한다면 ascending, 8부터 1까지 차례대로 연주한다면 descending, 둘 다 아니라면 mixed 이다.

연주한 순서가 주어졌을 때, 이것이 ascending인지, descending인지, 아니면 mixed인지 판별하는 프로그램을 작성하시오.

입력

첫째 줄에 8개 숫자가 주어진다. 이 숫자는 문제 설명에서 설명한 음이며, 1부터 8까지 숫자가 한 번씩 등장한다.

출력

첫째 줄에 ascending, descending, mixed 중 하나를 출력한다.

 

# My Answer

x = input().split()
if x == sorted(x):
    print("ascending")
elif x == sorted(x,reverse=True):
    print("descending")
else:
    print("mixed")

 

# Solution 1

a = list(map(int, input().split(' '))) 

ascending = True
descending = True

for i in range(1, 8): 
    if a[i] > a[i - 1]:
	descending = False 
    elif a[i] < a[i - 1]: 
    	ascending = False
        
if ascending: 
	print('ascending')
elif descending: 
	print('descending')
else: 
	print('mixed')

앞에서부터 비교해서 하나라도 내림차순이면 ascending 변수 = False

하나라도 오름차순이면 descending 변수 = False

 

최종 ascending = True 이면 모두 ascending,

최종 descending = True 이면 모두 descending,

모두 False 이면 mixed

 

반응형

'Algorithm > 백준 알고리즘 풀이' 카테고리의 다른 글

[Baekjoon] 10930. SHA-256 ( hashlib )  (0) 2021.04.15
[Baekjoon] 5397. 키로거  (0) 2021.04.15
[Baekjoon] 1966. 프린터 큐  (0) 2021.04.15
[Baekjoon] 1874. 스택 수열  (0) 2021.04.15
[Baekjoon] 2798. 블랙잭  (0) 2021.04.15