Kotlin/코딩테스트

[프로그래머스] 평행 - kotlin

깨노비 2023. 3. 9. 22:52
728x90
반응형

 

import kotlin.math.abs

class Solution {
    
    fun solution(dots: Array<IntArray>): Int {
        for (i in 1..3) {
            val gra1: Double = gradient(dots[0], dots[i])
            val result = when (i) {
                1 -> { gra1 == gradient(dots[2], dots[3])}
                2 -> { gra1 == gradient(dots[1], dots[3])}
                3 -> { gra1 == gradient(dots[1], dots[2])}
                else -> false
            }

            if (result) return 1
        }

        return 0
    }

    fun gradient(dot1: IntArray, dot2: IntArray): Double {
        return abs(dot1[0] - dot2[0]).toDouble() / abs(dot1[1] - dot2[1])    
    }
}
728x90
반응형