728x90
치킨 쿠폰
class Solution {
fun solution(chicken: Int): Int {
var chic = chicken
var answer: Int = 0
while (chic >= 10){
answer += chic / 10
chic = chic / 10 + chic % 10
}
return answer
}
}
이진수 더하기
import kotlin.math.*
class Solution {
fun solution(bin1: String, bin2: String): String = decimalToBinary(binaryToDecimal(bin1) + binaryToDecimal(bin2))
fun binaryToDecimal(binary: String): Int{
var result: Int = 0
var reverse = binary.reversed()
for (i in 0 until binary.length){
if (reverse[i] == '1'){
result += ((2.0).pow(i)).toInt()
}
}
return result
}
fun decimalToBinary(decimal: Int): String{
var result: String = ""
var num = decimal
println(num)
while (true){
var temp = num / 2
var remain = num % 2
result += remain.toString()
if (temp == 0) return result.reversed()
num = temp
}
}
}
A로 B 만들기
class Solution {
fun solution(before: String, after: String): Int = if (before.toList().sorted() == after.toList().sorted()) 1 else 0
}
k의 개수
class Solution {
fun solution(i: Int, j: Int, k: Int): Int {
var answer: Int = 0
for (it in i .. j){
if (it.toString().contains(k.toString())) answer += it.toString().count{it.toString() == k.toString()}
}
return answer
}
}
728x90