Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] (Swift) kakao - k์ง„์ˆ˜์—์„œ ์†Œ์ˆ˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ (lv.2)

๊ฐ์ž ๐Ÿฅ” 2022. 8. 22. 23:55
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ ๋งํฌ

https://school.programmers.co.kr/learn/courses/30/lessons/92335

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐ŸŸ  ๋ฌธ์ œ ํ’€์ด

1. k์ง„์ˆ˜๋กœ ๋ฐ”๊พธ๋ฉด์„œ ๋™์‹œ์— ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š”๋ฐ, 0์œผ๋กœ split ํ•ด์„œ ๋„ฃ์–ด์ฃผ๊ธฐ
2. splitํ•ด์„œ ๋„ฃ์–ด์ค€ ๋ฐฐ์—ด์—์„œ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์„œ ์†Œ์ˆ˜ ํŒ๋ณ„
  - 0์€ split ๋‹จ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— pass
  - 1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— pass
  - ์†Œ์ˆ˜ ํŒ๋ณ„ํ•ด์ค˜์„œ ์†Œ์ˆ˜๋ผ๋ฉด true, ์•„๋‹ˆ๋ผ๋ฉด false
3. ์†Œ์ˆ˜์ด๋ฉด count += 1  ํ•ด์คŒ

 

๐ŸŸ  ์†Œ์Šค์ฝ”๋“œ

https://github.com/deslog/Algorithm/blob/main/Algorithm/Programmers/kakao_k%EC%A7%84%EC%88%98%EC%97%90%EC%84%9C%20%EC%86%8C%EC%88%98%20%EA%B0%9C%EC%88%98%20%EA%B5%AC%ED%95%98%EA%B8%B0/main.swift

 

GitHub - deslog/Algorithm

Contribute to deslog/Algorithm development by creating an account on GitHub.

github.com

import Foundation

func solution(_ n: Int, _ k: Int) -> Int {
    let changeN = changeFormat(n: n, k: k)
    var count = 0

    for char in changeN {
        if char == 0 {
            continue
        }
        if char == 1 {
            continue
        }
        if isPrime(n: char) {
            count += 1
        }
    }
    return count
}

func isPrime(n: Int) -> Bool {
    if(n<4) {
        return n == 1 ? false : true
    }
    for i in 2...Int(sqrt(Double(n))) {
        if(n % i == 0) { return false }
    }
    return true
}

func changeFormat(n: Int, k: Int) -> [Int] {
    let split = String(n, radix: k).split(separator: "0").map { Int($0)! }
    return split
}
๋ฐ˜์‘ํ˜•