
헤맨 이유:
하나 하나 다 비교하고,
비교하는 와중에 "ascending"이나 "descending" 을 출력하려고 했다.
ascend = True, descend = True 로 미리 설정한 후
그 값이 거짓이면 False로 바꿔주고
비교가 다 끝나고 나서 True나 False 값을 체크해서 출력해주면 된다.
if arr[0] < arr[1] < arr[2] < arr[3] < arr[4] < arr[5] < arr[6] < arr[7] :
print("ascending")
을 만드는 코드를 짤 수 있었으면 더 좋았지만
ascend 와 descend를 True로 정해놓고
조건에 맞는다면 False로 바꿔주는 방법도 굉장히 좋은 것 같다.
내 풀이
arr = list(map(int,input().split()))
for i in range(len(arr)-1):
if arr[i] < arr[i+1]:
ascend = True
descend = False
elif arr[i] > arr[i+1]:
descend = True
ascend = True
else :
ascend = False
descend = False
if ascend == True and descend == False:
print("ascending")
elif ascend == False and descend == True:
print("descending")
else:
print("mixed")
다른 풀이
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")
input(). 입력을 받은 후
split(' ') 공백을 기준으로 나누기
map(int, ) map을 이용해서 각각을 int형으로 바꾸어준다.
그리고 리스트형으로 만든 후 arr에 담는다.
'⏰ 코딩테스트 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 - 1874 - 스택 수열 (0) | 2021.08.17 |
---|---|
백준 알고리즘 - 2798 - 블랙잭 (0) | 2021.08.16 |
백준 알고리즘 - 1000 - A+B (0) | 2021.06.28 |
백준 알고리즘 - 9663 - N Queen (0) | 2021.03.14 |
백준 알고리즘 - 15651 - N과 M (3) (0) | 2021.02.27 |