λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ“– Problem Solution/Programmers

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ μˆ˜λ°•μˆ˜λ°•μˆ˜λ°•μˆ˜λ°•μˆ˜λ°•μˆ˜? Swift

by Fomagran πŸ’» 2020. 2. 17.
728x90
λ°˜μ‘ν˜•

문제 μ„€λͺ…

길이가 n이고, μˆ˜λ°•μˆ˜λ°•μˆ˜λ°•μˆ˜....와 같은 νŒ¨ν„΄μ„ μœ μ§€ν•˜λŠ” λ¬Έμžμ—΄μ„ λ¦¬ν„΄ν•˜λŠ” ν•¨μˆ˜, solution을 μ™„μ„±ν•˜μ„Έμš”. 예λ₯Όλ“€μ–΄ n이 4이면 μˆ˜λ°•μˆ˜λ°•μ„ λ¦¬ν„΄ν•˜κ³  3이라면 μˆ˜λ°•μˆ˜λ₯Ό λ¦¬ν„΄ν•˜λ©΄ λ©λ‹ˆλ‹€.

μ œν•œ 쑰건

  • n은 길이 10,000μ΄ν•˜μΈ μžμ—°μˆ˜μž…λ‹ˆλ‹€.

μž…μΆœλ ₯ 예

n return
3 μˆ˜λ°•μˆ˜
4 μˆ˜λ°•μˆ˜λ°•

풀이: 짝수일 땐 μˆ˜λ°•μ„ λ°˜λ³΅ν•˜κ³  ν™€μˆ˜μΌ 땐 μˆ˜κ°€ μžˆλŠ” μƒνƒœμ—μ„œ λ°•μˆ˜λ₯Ό λ°˜λ³΅ν•΄μ„œ 더해쀀닀.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func solution(_ n:Int-> String {
    var str = String()
     var su = "수"
    if n == 1 {
        return "수"
    }
    else if n%2 == 0 {
        for _ in 1...n/2{
            str += "μˆ˜λ°•"
        }
        return str
    }else{
        for _ in 1...n/2{
            su += "λ°•μˆ˜"
        }
        return su
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
728x90
λ°˜μ‘ν˜•

λŒ“κΈ€