반응형
문제
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
풀이
이 문제는 입력받은 배열이
- 1, 2, 3, 4, 5, 6, 7, 8 과 같이 1부터 8까지 오름차순으로 되어있으면 ascending
- 8, 7, 6, 5, 4, 3, 2, 1 과 같이 8부터 1까지 내림차순으로 되어있으면 descending
- 그 외 mixed
를 출력하면 되는 문제
소스코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = readLine()!.split(separator: " ").map { Int($0)! } | |
if array == array.sorted(by: <) { | |
print("ascending") | |
} else if array == array.sorted(by: >) { | |
print("descending") | |
} else { | |
print("mixed") | |
} |
후기
sorted 메서드로 쉽게 풀 수 있는 문제
굳이 sorted를 사용하지 않고,
[1, 2, 3, 4, 5, 6, 7, 8], [8, 7, 6, 5, 4, 3, 2, 1] 인지 비교해도 무방하다.
반응형
'PS > 백준' 카테고리의 다른 글
[BOJ] 백준 2635 수 이어가기 (Swift) (1) | 2023.12.27 |
---|---|
[BOJ] 백준 27866 문자와 문자열 (Swift) (1) | 2023.12.19 |
[BOJ] 백준 2741 N 찍기 (Swift) (0) | 2023.12.11 |
[BOJ] 백준 2577 숫자의 개수 (Swift) (1) | 2023.12.11 |
[BOJ] 백준 2475 검증수 (Swift) (0) | 2023.12.11 |