Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- Lamda
- rds
- 병목
- SageMaker
- terraform
- fcm
- cloudwatch
- kubernetes
- aws
- lambda
- amazonqcli
- IaC
- Validation
- CHECK
- sns
- serverless
Archives
- Today
- Total
잡다한 IT 지식
백준 - 시리얼 번호(Kotlin) 본문
본 알고리즘 풀이는 Routine Study에서 진행하고 있습니다.
https://github.com/ROUTINE-STUDY/Algorithm
저를 포함한 구성원이 대부분 초보이므로, 원하시는분은 언제라도 들어오셔도 좋습니다.
GitHub - ROUTINE-STUDY/Algorithm: 초보 알고리즘 스터디 / 누구나 참여 가능
초보 알고리즘 스터디 / 누구나 참여 가능 :runner:. Contribute to ROUTINE-STUDY/Algorithm development by creating an account on GitHub.
github.com
문의는 댓글 바람.
문제 출처 :https://www.acmicpc.net/problem/1431
[문제 설명]
그냥 정렬 문제
[접근 방법]
그냥 정렬 조건에 맞춰 정렬하는 문제입니다.
예전에 정렬 조건을 전부 체크 안해서 엄청 큰 낭패를 본 적이 있는데 이번엔 그 당시 경험을 살려서 간단하게 완료했습니다.
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.out))
val cntOfSerial = br.readLine().toInt()
val listOfSerial = ArrayList<Serial>()
for (idx in 0 until cntOfSerial) {
listOfSerial.add(Serial(br.readLine()))
}
listOfSerial.sort()
listOfSerial.forEach {
bw.write(it.no)
bw.write("\n")
}
bw.flush()
}
class Serial(
val no: String
) : Comparable<Serial> {
override fun compareTo(other: Serial): Int {
if (this.no.length > other.no.length) {
return 1
} else if (this.no.length < other.no.length) {
return -1
}
val thisSum = this.no.filter { it in '0'..'9' }.sumOf { it.digitToInt() }
val otherSum = other.no.filter { it in '0'..'9' }.sumOf { it.digitToInt() }
if (thisSum > otherSum) {
return 1
} else if (thisSum < otherSum) {
return -1
}
return this.no.compareTo(other.no)
}
}
'알고리즘 문제 풀이' 카테고리의 다른 글
프로그래머스 - 푸드 파이트(Kotlin) (0) | 2022.11.11 |
---|---|
2022 Winter Coding - 겨울방학 스타트업 인턴 프로그램 후기 (2) | 2022.11.05 |
프로그래머스 - 베스트 앨범(Kotlin) (0) | 2022.08.09 |
프로그래머스 - 예상 대진표(Kotlin) (0) | 2022.08.09 |
백준 - 패션왕 신해빈(Kotlin) (0) | 2022.07.26 |