πŸ’»μ½”λ“œμΉ΄νƒ€

MunkiJeonΒ·2024λ…„ 5μ›” 31일

μ½”λ”©ν…ŒμŠ€νŠΈ

λͺ©λ‘ 보기
13/18
post-thumbnail

038.μ§μ‚¬κ°ν˜• 별찍기

/*
문제 μ„€λͺ…
이 λ¬Έμ œμ—λŠ” ν‘œμ€€ μž…λ ₯으둜 두 개의 μ •μˆ˜ nκ³Ό m이 μ£Όμ–΄μ§‘λ‹ˆλ‹€.
별(*) 문자λ₯Ό μ΄μš©ν•΄ κ°€λ‘œμ˜ 길이가 n, μ„Έλ‘œμ˜ 길이가 m인 μ§μ‚¬κ°ν˜• ν˜•νƒœλ₯Ό 좜λ ₯ν•΄λ³΄μ„Έμš”.

μ œν•œ 쑰건
nκ³Ό m은 각각 1000 μ΄ν•˜μΈ μžμ—°μˆ˜μž…λ‹ˆλ‹€.
*/
fun main(args: Array<String>) {
    val (a, b) = readLine()!!.split(' ').map(String::toInt)
    //readLine() = μž…λ ₯κ°’ String으둜 λ°›μŒ, !!= λ°˜λ“œμ‹œ μ‘΄μž¬ν•¨
    //.split(' ') = ' '으둜 문자 잘라 λ°°μ—΄ λ§Œλ“¬
    //.map(String::toInt) = 자λ₯Έ 배열을 각각 a,b λ³€μˆ˜μ— λ§΅ν•‘ 함
    var str =""
    for (i in 1 .. b){
        for (j in 1..a){
            str += "*"
        }
        println("${str}")
        str =""
    }
}

039.μ΅œλŒ€κ³΅μ•½μˆ˜μ™€ μ΅œμ†Œκ³΅λ°°μˆ˜
> 머리 써가며 κΈ°λ³Έ λ¬Έλ²•λ§Œ μ“°λŠ”κ±° 보단 μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•΄ 보고자 검색후 Try....

/*
문제 μ„€λͺ…
두 수λ₯Ό μž…λ ₯λ°›μ•„ 두 수의 μ΅œλŒ€κ³΅μ•½μˆ˜μ™€ μ΅œμ†Œκ³΅λ°°μˆ˜λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜, solution을 μ™„μ„±ν•΄ λ³΄μ„Έμš”. λ°°μ—΄μ˜ 맨 μ•žμ— μ΅œλŒ€κ³΅μ•½μˆ˜, κ·Έλ‹€μŒ μ΅œμ†Œκ³΅λ°°μˆ˜λ₯Ό λ„£μ–΄ λ°˜ν™˜ν•˜λ©΄ λ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ 두 수 3, 12의 μ΅œλŒ€κ³΅μ•½μˆ˜λŠ” 3, μ΅œμ†Œκ³΅λ°°μˆ˜λŠ” 12μ΄λ―€λ‘œ solution(3, 12)λŠ” [3, 12]λ₯Ό λ°˜ν™˜ν•΄μ•Ό ν•©λ‹ˆλ‹€.

μ œν•œ 사항
두 μˆ˜λŠ” 1이상 1000000μ΄ν•˜μ˜ μžμ—°μˆ˜μž…λ‹ˆλ‹€.
*/
import kotlin.math.*
class Solution {
    fun solution(n: Int, m: Int): IntArray {
        return intArrayOf(gcm(n, m), lcm(n, m))
    }
    fun gcm(a: Int, b: Int): Int { //μ΅œλŒ€ κ³΅μ•½μˆ˜ - gcmμ•Œκ³ λ¦¬μ¦˜
        var maximum = max(a, b)//max() = 두 μˆ˜μ€‘ 제일 큰수
        var minimum = min(a, b)//min() = 두 μˆ˜μ€‘ 제일 μž‘μ€μˆ˜
		
        //μž¬κ·€ν•¨μˆ˜ μ‚¬μš© = 좜λ ₯μ—μ„œ ν•¨μˆ˜ 본인을 λ‹€μ‹œ 뢈러 μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜λ₯Ό 말함
        return if ( minimum != 0) { gcm(minimum, maximum % minimum) } 
        else { maximum }
    }
    //μ΅œμ†Œ 곡배수 - lcmμ•Œκ³ λ¦¬μ¦˜
    fun lcm(a: Int, b: Int): Int = (a * b) / gcm(a, b)
}
profile
곡μž₯μžλ™ν™”μ™€ μ›Ή κ°œλ°œμ„ ν•₯μ•„ λ¨Ήλ‹€ μ•± 개발둜 μ „ν–₯ν•œ 개발자의 ν‚€λ³΄λ“œμ˜ 낑은 킀캑⛑️

0개의 λŒ“κΈ€