[Kotlin] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐ์ผ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 11~15
๋ฐ˜์‘ํ˜•

11. ์ง์ˆ˜์™€ ํ™€์ˆ˜ 
12. ํ‰๊ท  ๊ตฌํ•˜๊ธฐ 
13. ์ž๋ฆฟ์ˆ˜ ๋”ํ•˜๊ธฐ 
14. ์•ฝ์ˆ˜์˜ ํ•ฉ 
15. ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋˜๋Š” ์ˆ˜ ์ฐพ๊ธฐ

 


 

 

11. ์ง์ˆ˜์™€ ํ™€์ˆ˜ ๋งํฌ

 

 

class Solution {
    fun solution(num: Int): String {
        if(num % 2 == 0){
            return "Even"
        } else {
            return "Odd"
        }
        
    }
}

 

 

 

 

12. ํ‰๊ท  ๊ตฌํ•˜๊ธฐ ๋งํฌ

 

 

class Solution {
    fun solution(arr: IntArray): Double {
        var answer : Double = 0.0
        answer = arr.average()
        return answer
    }
}
class Solution {
    fun solution(arr: IntArray): Double 
    {
        return arr.average()
    }
}

 

 

 

 

13. โญ ์ž๋ฆฟ์ˆ˜ ๋”ํ•˜๊ธฐ ๋งํฌ

 

 

์ฒ˜์Œ์—” ์ฝ”๋“œ๋ฅผ ํ•ด๊ดด๋ง์ธกํ•˜๊ฒŒ ์งฐ๋‹ค. (์ž‘๋™ ์•ˆ ํ•จ)

class Solution {
    fun solution(n: Int): Int {
        
        
        var three = {n - (n % 100)} / 100 
        var two = {n % 100 - ((n % 100) % 10)} / 10
        var one = (n % 100) % 10
        
        return three+two+one
        
    }
}
๋‹ค์Œ์€ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

class Solution {
    fun solution(n: Int): Int {
        val list = n.toString().split("")
        var result = 0
        for (i in list) {
            if (i != "") {
                result += i.toInt()
            }
        }

        return result
    }
}
์ด๋ ‡๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

class Solution {
    fun solution(n: Int): Int = n.toString().toList().sumOf { it.digitToInt() }
}
class Solution {
    fun solution(n: Int): Int {
        var input = n
        var answer = 0

        while (input != 0) {
            answer += input % 10
            input /= 10
        }

        return answer
    }
}

 

  • ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: ์ž…๋ ฅ๋œ ์ˆซ์ž๋ฅผ String์œผ๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•œ ๋’ค, ๋ฌธ์ž์—ด์„ ์ž˜๋ผ์„œ ๋‹ค์‹œ intํ˜•์œผ๋กœ ๋ณ€ํ™˜์‹œํ‚จ ๋’ค ๋”ํ–ˆ๋‹ค.
  • ์„ธ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: ์ž…๋ ฅ๋œ ์ˆซ์ž๋ฅผ String์œผ๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•œ ๋’ค,
    toList() ํ•จ์ˆ˜๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ณต์‚ฌํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ 
    sumof๋กœ ํ•ด๋‹น ๋ฐฐ์—ด(it)์˜ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค. ( digitToInt()๋Š” ๋ฌธ์ž ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ์ˆซ์ž์ธ ๊ฒฝ์šฐ Int ์ˆซ์ž๋กœ ๋ณ€ํ™˜)
  • ๋„ค ๋ฒˆ์งธ ๋ฐฉ๋ฒ•: ๊ทธ๋ƒฅ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ• (์ •์ˆ˜/์ •์ˆ˜ = ์ •์ˆ˜๋ฅผ ์ด์šฉํ•ด 10์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•˜๊ณ , 10์„ ๋‚˜๋ˆ„๋Š” ๊ฒƒ ๋ฐ˜๋ณต)

 

 

 

14. ์•ฝ์ˆ˜์˜ ํ•ฉ ๋งํฌ

 

 

๋‚ด๊ฐ€ ํ•œ ํ’€์ด

class Solution {
    fun solution(n: Int): Int {
        var sum = 0
        
         for (i: Int in 1..n){
             if(n % i == 0){
                 sum += i
             }
         }
        return sum
    }
}
๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

class Solution {
    fun solution(n: Int): Int {
        var answer = 0

        answer = (1..n).filter {
            n % it == 0
        }.sum()
        return answer
    }
}

 

 

 

 

15. ๐Ÿ’ก ๋‚˜๋จธ์ง€๊ฐ€ 1์ด ๋˜๋Š” ๊ฐ’ ์ฐพ๊ธฐ ๋งํฌ

 

 

๋‚˜์˜ ์ฝ”๋“œ. ์ž‘๋™ ์•ˆ ๋จ

class Solution {
    fun solution(n: Int): Int {
        
        for (i: Int in 2..n-1){
            if (n % i == 1) return i
        }
            
    }
}
class Solution {
    fun solution(n: Int): Int {
        
        for (i:Int in 2..n-1){
            if (n % i == 1) return i
        }
            return 0    // ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ด„ํ˜ธ ์•ˆ์— return 0์„ ๋„ฃ์–ด์•ผ ํ–ˆ๋‹ค. 
    }
}
  • 2๋ถ€ํ„ฐ n-1 ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ ์ด์œ ๋Š” 1๊ณผ n์„ ๋นผ๊ธฐ ์œ„ํ•ด์„œ์ž„

 

 

๐Ÿ’ก๊ผญ return 0 ์„ ๋„ฃ์–ด์•ผ ํ• ๊นŒ?

  • ํ˜„์žฌ for๋ฌธ ์•ˆ์— if๊ฐ€ ์žˆ๊ณ  ๊ทธ ์•ˆ์— return(๋ฐ˜ํ™˜๊ฐ’)์ด ์žˆ์Œ
  • ๋ฉ”์ธ ํ•จ์ˆ˜์ธ solution ํ•จ์ˆ˜๋ฅผ ๋ณด๋ฉด ๋ฐ˜ํ™˜๊ฐ’์„ Int๋กœ ์ •์˜ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, else์˜ ๊ฒฝ์šฐ ์ปดํ“จํ„ฐ๋Š” ๋ฐ˜ํ™˜๊ฐ’์„ ๊ฐ–์ง€ ๋ชปํ•จ
  • ๋”ฐ๋ผ์„œ ํ˜•์‹์ ์œผ๋กœ๋ผ๋„ ๊ทธ ์ด์™ธ์˜ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์ •์ˆ˜ํ˜• ๋ฐ˜ํ™˜๊ฐ’ return 0์„ ์„ค์ •

 

 

 

 

 

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T