본문 바로가기
  • Let's study
PS/Baekjoon

[백준] 27159: 노 땡스!(Kotlin)

by 코딩고수이고파 2025. 10. 9.

문제

https://www.acmicpc.net/problem/27159

풀이

1. 리스트를 정렬하고 첫 번째 값을 ans에 먼저 저장한다.

2. 이후 인덱스 1부터 배열의 i번째 값에서 1을 뺀 것이 배열의 i-1번째 값보다 크면 (list[i]-1 > list[i-1]) 차가 1이 아니므로, ans에 배열의 i번째 값을 더한다.

코드

fun main() {
	val N=readLine()!!.toInt()

	val list = readLine()!!.split(" ").map{it.toInt()}
	list.sorted()
	
	var ans=list[0];
	
	for(i in 1 until N){
		if(list[i]-1>list[i-1]){
			ans+=list[i]
		}
	}
	
	println(ans)
    
}

댓글