본문 바로가기

<TIL>/BaekJoon_문제풀이

[2902번]_음계_easy

문제제목: 음계

문제난이도: 하

유형: 배열, 구현

적정풀이시간: 15분


zzt님의 12496803번 소스 코드 (acmicpc.net)

print({"2345678":"ascending","7654321":"descending"}.get(input()[2::2],"mixed"))

역시 세상을 넓고 천재는 많구나.

이렇게 짧은 한 줄의 코드로 해결할 생각을 하다니 대단한 것 같다.


나의 풀이

def check():
    input_li = input()
    pitches = list(map(int,input_li.split(' ')))
    
    ascending = True
    descending = True
    
    for num in range(len(pitches)-1):
        if pitches[num] < pitches[num+1]:
            descending = False
        else:
            ascending = False
        if not ascending and not descending:
            return 'mixed'
    if ascending and not descending:
        return 'ascending'
    else:
        return 'descending'
            
print(check())

개선된 풀이

def check():
    pitches = list(map(int,input().split(' ')))
    
    asc, desc, num = False, False, 1
    
    while not asc or not desc:
        if num >= len(pitches):
            break
            
        if pitches[num-1] - 1 == pitches[num]:
            desc = True
            num += 1
            
        elif pitches[num-1] + 1 == pitches[num]:
            asc = True
            num += 1      
        
        else:
            break
            
    if not asc and not desc:
        return 'mixed'
        
    return 'ascending' if asc else 'descending'
    
print(check())

'<TIL> > BaekJoon_문제풀이' 카테고리의 다른 글

[10930번]_SHA-256  (0) 2021.05.04
[1966번]_프린터 큐  (0) 2021.05.02
5단계(실습1)_10817번_세 수  (0) 2020.09.13
5단계(실습1)_5543번_상근날드  (0) 2020.09.13
5단계(실습1)_10039번_평균 점수  (0) 2020.09.13