Programmers/Lv. 0 (完)

[Kotlin] Programmers 코딩테스트 입문 Day 22 dp, 수학, 조건문, 배열

chattymin 2023. 6. 25. 10:41
728x90
반응형

저주의 숫자 3

class Solution {
    fun solution(n: Int): Int {
        var result: MutableList<Int> = mutableListOf(0)

        repeat(n){ index ->
            var temp = result[index] + 1
            while (temp.toString().contains("3") || temp % 3 == 0) temp++
            result.add(temp)
        }

        return result.last()
    }
}

 

 

 

평행

import java.lang.Math.abs
class Solution {
    fun solution(dots: Array<IntArray>): Int {
        val result: HashSet<Double> = hashSetOf()
        val fir = abs(dots[0].first().toDouble() - dots[1].first()) /  abs(dots[0].last().toDouble() - dots[1].last())
        val fir2 = abs(dots[2].first().toDouble() - dots[3].first()) /  abs(dots[2].last().toDouble() - dots[3].last())
        val sec = abs(dots[0].first().toDouble() - dots[2].first()) /  abs(dots[0].last().toDouble() - dots[2].last())
        val sec2 = abs(dots[2].first().toDouble() - dots[3].first()) /  abs(dots[1].last().toDouble() - dots[3].last())
        val thi = abs(dots[0].first().toDouble() - dots[3].first()) /  abs(dots[0].last().toDouble() - dots[3].last())
        val thi2 = abs(dots[1].first().toDouble() - dots[2].first()) /  abs(dots[1].last().toDouble() - dots[2].last())

        if (fir == fir2) return 1
        if (sec == sec2) return 1
        if (thi == thi2) return 1

        return 0
    }
}

 

 

 

겹치는 선분의 길이

class Solution {
    fun solution(lines: Array<IntArray>): Int {
         // 1. arr 배열 및 변수 초기화
        val arr = IntArray(200)
        var answer = 0

        // 2. lines 정보를 arr 배열에 적용
        for (line in lines) for (j in line[0] + 100 until line[1] + 100) arr[j]++

        // 3. arr 배열에서 겹친 부분 세기
        answer = arr.filter { it > 1 }.count()
        return answer
    }
}

 

 

 

유한소수 판별하기

class Solution {
    fun solution(a: Int, b: Int): Int {
        var newB = b / gcd(a, b)
        while (newB != 1) {
            newB /= if (newB % 2 == 0) {
                2
            } else if (newB % 5 == 0) {
                5
            } else {
                return 2
            }
        }
        return 1
}
    tailrec fun gcd(a: Int, b: Int): Int {
        return if (b == 0) {
            a
        } else {
            gcd(b, a % b)
        }
    }
}
728x90
반응형