본문 바로가기
Python3/백준 알고리즘

2022.02.22 [백준] (python 파이썬) 음계

by ian's coding 2022. 2. 22.
728x90
반응형

 

https://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

 


풀이

2개의 풀이 방법으로 해결했다. 

1번째 코드는 for문을 이용하여 하나하나 비교하는 방식으로 작성했고, 2번째 코드는 출력문에서 각 조건을 바로 확인해서 출력하는 코드로 작성했다.

 

- 1번 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
arr=list(map(int, input().split()))
 
check=True
if arr[0]==1:
    for i in range(1,len(arr)):
        if arr[i]-arr[i-1]!=1:
            check=False
            break
    if check:
        print('ascending')
    else:
        print('mixed')
elif arr[0]==8:
    for i in range(1,len(arr)):
        if arr[i]-arr[i-1]!=-1:
            check=False
            break
    if check:
        print('descending')
    else:
        print('mixed')
else:
    print('mixed')
cs

 

- 2번 코드

1
2
3
4
arr=input()
 
print('ascending' if arr=='1 2 3 4 5 6 7 8' else 'descending' if arr=='8 7 6 5 4 3 2 1' else 'mixed')
 
cs

 

 

728x90
반응형

댓글