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

1. ๋‘ ์ˆ˜์˜ ์ฐจ
2. ๋‘ ์ˆ˜์˜ ๊ณฑ
3. ๋ชซ ๊ตฌํ•˜๊ธฐ
4. ๋‚˜์ด ์ถœ๋ ฅ
5. ์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ
6. ๋‘ ์ˆ˜์˜ ํ•ฉ
7. ๋‘ ์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ
8. ๊ฐ๋„๊ธฐ
9. ์ง์ˆ˜์˜ ํ•ฉ
10. ๋ฐฐ์—ด์˜ ํ‰๊ท ๊ฐ’

 


 

1. ๋‘ ์ˆ˜์˜ ์ฐจ ๋งํฌ

class Solution {
    fun solution(num1: Int, num2: Int): Int {
        return(num1 - num2)
    }
}

 

๐Ÿ’ก ๋ฏธํ•ด๊ฒฐ : ์˜ˆ์™ธ์ฒ˜๋ฆฌ์—์„œ์˜ ๋ถ€๋“ฑํ˜ธ๊ฐ€ ๊ถ๊ธˆํ•˜๋‹ค.
0 <= age <= 120; ์™€ ๊ฐ™์€ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ๊ฐ€?

 

2. ๋‘ ์ˆ˜์˜ ๊ณฑ ๋งํฌ

class Solution {
    fun solution(num1: Int, num2: Int): Int {
        var answer: Int = 0
        answer = num1 * num2
        return answer
    }
}

 

 

3. ๋ชซ ๊ตฌํ•˜๊ธฐ ๋งํฌ

fun solution(num1: Int, num2: Int): Int {
    return num1 / num2
}

 

(์ฐธ๊ณ 1) num2 ๊ฐ€ 0 ์ผ ๊ฒฝ์šฐ , Kotlin์€ ArithmeticException ์„ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
๋”ฐ๋ผ์„œ ์‹ค์ œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” num2๊ฐ€ 0์ธ์ง€ ํ™•์ธํ•˜๊ณ  ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

(์ฐธ๊ณ 2) Kotlin์˜ ์ •์ˆ˜ ๋‚˜๋ˆ—์…ˆ์€ ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฐ๋‹ค. ๋”ฐ๋ผ์„œ ์ •ํ™•ํ•œ ๋ชซ๋งŒ ํ•„์š”ํ•  ๋•Œ ์ ํ•ฉํ•˜๋‹ค.

 

 

4. ๋‚˜์ด ์ถœ๋ ฅ ๋งํฌ

class Solution {
    fun solution(age: Int): Int {
        val a = age - 1
        return 2022 - a - 1
    }
}
class Solution {
    fun solution(age: Int) = 2023 - age
}

 

5. ์ˆซ์ž ๋น„๊ตํ•˜๊ธฐ ๋งํฌ

class Solution {
    fun solution(num1: Int, num2: Int): Int {
      return if (num1 == num2) 1 else -1
    }
}

 

 

6. ๋‘ ์ˆ˜์˜ ํ•ฉ ๋งํฌ

class Solution {
    fun solution(num1: Int, num2: Int) = num1 + num2
}
package com.example.codekata

    fun main() {
        var num1 = readln().toInt()
        var num2 = readln().toInt()
        if (num1 or num2 < -50000){
            println("-50000์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์จ์ฃผ์„ธ์š”")
        }
        else if (num1 or num2 > 50000){
            println("50000์ดํ•˜์˜ ์ˆซ์ž๋ฅผ ์จ์ฃผ์„ธ์š”")
        }
        else
        println("${solution(num1,num2)}")

    }

fun solution(num1: Int, num2: Int): Int {
    var answer: Int = 0
    answer = num1 + num2
    return answer
}

 

 

7. ๋‘ ์ˆ˜์˜ ๋‚˜๋ˆ—์…ˆ ๋งํฌ

class Solution {
    fun solution(num1: Int, num2: Int): Int {
        var answer: Int = 0
        answer = (num1.toDouble()/num2.toDouble()*1000).toInt()
        return answer
    }
}

 

๐Ÿ’ก num1๊ณผ num2์˜ ์ž๋ฃŒํ˜•์„ toDouble()๋กœ ๋ณ€ํ™˜ ์‹œํ‚ค๋Š” ์ด์œ :
์ •์ˆ˜ ๋‚˜๋ˆ„๊ธฐ ์ •์ˆ˜๋Š” ์ •์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ต์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค.  3 ๋‚˜๋ˆ„๊ธฐ 2๋Š” 1.5์ง€๋งŒ, Kotlin์œผ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด 1์ด ๋˜์–ด๋ฒ„๋ฆฐ๋‹ค.

๐Ÿ’ก ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ toDouble()๋ง๊ณ ๋„, ์‹ค์ˆ˜๋ฅผ ๊ณฑํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค (ex. num1 * 1.0 ํ•ด์คŒ)

 

 

8. ๊ฐ๋„๊ธฐ ๋งํฌ

 

//๋‚˜์˜ ํ’€์ด

class Solution {
    fun solution(angle: Int): Int {
        var answer: Int = 0
        if ( 0 <= angle && angle < 90) {
            return 1
        }
        else if(angle == 90){
            return 2
        }
        else if( 90 <= angle && angle < 180){
            return 3
        }
        else {
            return 4
        }
    }
}

 

// ๋‹ค๋ฅธ ๋ถ„์˜ ํ’€์ด


package com.example.codekata

fun main() {
    var num1 = readln().toInt()
    if (num1 <= 0){
        println("0๋ณด๋‹คํฐ ์ˆซ์ž๋ฅผ ์จ์ฃผ์„ธ์š”")
    }
    else if (num1  > 180){
        println("180์ดํ•˜์˜ ์ˆซ์ž๋ฅผ ์จ์ฃผ์„ธ์š”")
    }
    else
        println("${solution(num1)}")

}
fun solution(angle: Int): Int {
        var answer: Int = 0
    if (angle < 90){
        answer = 1
    }
    else if (angle == 90){
        answer = 2
    }
    else if (angle < 180){
        answer =3
    }
    else {
        answer = 4
    }
        return answer
}

 

  • else์—๋Š” ์กฐ๊ฑด์„ ๋„ฃ์ง€ ์•Š์Œ์„ ์ฃผ์˜ํ•˜๊ธฐ (else์— ์กฐ๊ฑด ๊ด„ํ˜ธ ๋‹ฌ๋ฉด ์˜ค๋ฅ˜๋‚จ)
  • ๊ฐ™์Œ์„ ํ‘œ์‹œํ•˜๋Š” ๋ถ€๋“ฑํ˜ธ๊ฐ€ ==์„ ๋ง๊ฐํ•˜์ง€ ์•Š๊ธฐ. ํ•ด๋‹น if๋ฌธ์—์„œ ๋ถ€๋“ฑํ˜ธ ==์ด ์•„๋‹ˆ๋ผ =์ด๋ฉด ์˜ค๋ฅ˜๋‚จ

 


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

 

 

์•„๋ž˜ ๋‚ด์šฉ์œผ๋กœ ๊ณ„์‚ฐ์ด ์ •์ƒ์ ์œผ๋กœ ์•ˆ ๋˜์–ด์„œ ํ•œ์ฐธ์„ ๊ณ ๋ฏผํ•จ

class Solution {
    fun solution(n: Int): Int {
        var answer: Int = 0
        var sum: Int = 0
        
        if(2 <= n && n <= 1000){
            for(i: Int in 2..n){
               if (i % 2 == 0)
                  sum =+ i
            }
        }
        return sum
    }
    
}
class Solution {
    fun solution(n: Int): Int {
        var answer: Int = 0
        var sum: Int = 0
        
        if(2 <= n && n <= 1000){
            for(i: Int in 2..n){
               if (i % 2 == 0)
                  sum = sum + i   /// sum =+ i ์—์„œ sum = sum + i๋กœ ๋ณ€๊ฒฝํ•˜๋‹ˆ ํ•ฉ๊ฒฉ
                                  /// sum +=๋ฅผ =+๋กœ ์ž˜๋ชป ์จ์„œ ์ƒ๊ธด ๋ฌธ์ œ
            }
        }
        return sum
    }
    
}
  • += ์„ =+๋กœ ์ž˜๋ชป ์จ์„œ ์ƒ๊ธด ๋ฌธ์ œ
  • ํ—ท๊ฐˆ๋ฆฌ๋ฉด ๊ทธ๋ƒฅ sum = sum + i ์“ฐ์ž
  • ๊ตณ์ด ์ •์ˆ˜ํ˜• ๋ณ€์ˆ˜ sum์„ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ , answer += i ๋กœ ์ ๊ณ  return answer ํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•ด์ง
  • Exception in thread "main" java.lang.ArithmeticException: / by zero ๊ณผ ๊ฐ™์€ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜?
    (1) ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํŒŒ์ผ์— ์ ‘๊ทผํ•˜๋ ค ํ•˜๊ฑฐ๋‚˜
    (2) ์ •์ˆ˜๋ฅผ 0์œผ๋กœ ๋‚˜๋ˆ„๋Š” ํ–‰๋™ 
    (3) ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋ฅผ ์ž˜๋ชป ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋“ฑ

 

 


10. ๋ฐฐ์—ด์˜ ํ‰๊ท ๊ฐ’ ๋งํฌ

 

 

class Solution {
    fun solution(numbers: IntArray): Double {
    
        var sum = 0
        for (i in numbers){
            sum += i
        }    
        
        return sum.toDouble() / numbers.size
    }
}
class Solution {
    fun solution(numbers: IntArray): Double {
         
        return numbers.average()
        
    }
}

 

  • ๋ฐฐ์—ด๋ช….size ๋Š” ๋ฐฐ์—ด ์•ˆ ์›์†Œ์˜ ๊ฐœ์ˆ˜
  • ์ฝ”ํ‹€๋ฆฐ์—์„œ ์ œ๊ณตํ•˜๋Š” average()๋ฅผ ์“ฐ๋ฉด ๋” ๋นจ๋ฆฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Œ
    average()๋Š” ์ปฌ๋ ‰์…˜์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž ์›์†Œ๋“ค์˜ ํ‰๊ท ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T