알고리즘 문제 풀이/프로그래머스
[Algorithm] 프로그래머스 - 주사위 게임 3 (Kotlin)
참깨빵위에참깨빵_
2023. 12. 31. 00:34
728x90
반응형
풀지 못해서 다른 사람의 풀이를 참고했다.
import java.util.*
class Solution {
fun solution(a: Int, b: Int, c: Int, d: Int): Int {
var answer = 0
val nums = IntArray(4)
nums[0] = a
nums[1] = b
nums[2] = c
nums[3] = d
var multi = 1
Arrays.sort(nums)
val dice = IntArray(6)
for (i in 0..3) {
dice[nums[i] - 1]++
}
for (i in 0..5) {
if (dice[i] == 4) {
answer = 1111 * (i + 1)
break
} else if (dice[i] == 3) {
for (j in 0..5) {
if (dice[j] == 1) {
answer = (10 * (i + 1) + (j + 1)) * (10 * (i + 1) + (j + 1))
break
}
}
} else if (dice[i] == 2) {
for (j in 0..5) {
if (dice[j] == 2) {
if (i == j) {
continue
} else {
answer = (i + 1 + j + 1) * (i + 1 - (j + 1))
break
}
} else if (dice[j] == 1) {
multi *= (j + 1)
}
}
if (multi != 1) {
answer = multi
}
}
if (nums[0] != nums[1] && nums[1] != nums[2] && nums[2] != nums[3]) {
answer = nums[0]
}
}
return answer
}
}
반응형