Bibi's DevLog ๐Ÿค“๐ŸŽ

[๋ฐฑ์ค€ 18312] ์‹œ๊ฐ (Swift) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด Swift

[๋ฐฑ์ค€ 18312] ์‹œ๊ฐ (Swift)

๋น„๋น„ bibi 2022. 12. 13. 15:20

์ด์ฝ”ํ…Œ(์ด๊ฒƒ์ด ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋‹ค) ๊ตฌํ˜„ ์˜ˆ์ œ ์ค‘ ํ•˜๋‚˜. (113ํŽ˜์ด์ง€)

18312๋ฒˆ: ์‹œ๊ฐ

  • 0-n์‹œ, 0-59๋ถ„, 0-59์ดˆ ์‚ฌ์ด์˜ ๋ชจ๋“  ์‹œ๊ฐ„์„ ์‚ดํŽด๋ด์•ผ ํ•˜๋ฏ€๋กœ 3์ค‘ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ตฌํ•œ๋‹ค.
  • ์ฃผ์˜ํ•  ์ ์€, ํ•œ ์ž๋ฆฌ ์ˆซ์ž์ผ ๋•Œ ์•ž์— 0์ด ์ƒ๊ธฐ๋Š” ๊ฒƒ๊นŒ์ง€ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์•ผ K๊ฐ€ 0์ธ ๊ฒฝ์šฐ๋ฅผ ๋†“์น˜์ง€ ์•Š๊ณ  ์…€ ์ˆ˜ ์žˆ๋‹ค.
    • ๋‚˜๋Š” appendZero๋ผ๋Š” ๋ณ„๋„ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ–ˆ๋‹ค.

ํ’€์ด1(69104KB, 56ms)

let input = readLine()!.split(separator: " ")
let n = Int(input[0])!
let k = Character(String(input[1]))
var answer = 0

func appendZero(to num: Int) -> String {
    var answer = ""
    if num < 10 {
        answer = "0\(num)"
    } else {
        answer = "\(num)"
    }
    return answer
}

(0...n).forEach { hour in
    (0...59).forEach { minute in
        (0...59).forEach { second in
            let time = "\(appendZero(to: hour))\(appendZero(to: minute))\(appendZero(to: second))"
            if time.contains(k) {
                answer += 1
            }
        }
    }
}
print(answer)

ํ’€์ด2(69104KB, 12ms)

๋‹ค๋ฅธ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ์‹œ, ๋ถ„, ์ดˆ ๋ชจ๋‘ 2์ž๋ฆฌ์ž„์„ ์ด์šฉํ•ด ์‹ญ์˜ ์ž๋ฆฌ(= 10์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ’)๊ณผ ์ผ์˜ ์ž๋ฆฌ(10์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€)๋ฅผ ์ผ์ผ์ด ๋น„๊ตํ•ด ๊ตฌํ–ˆ๋‹ค.

์ด์ชฝ์€ ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ˆœ์ˆ˜ ์—ฐ์‚ฐ์œผ๋กœ ๊ตฌํ•  ์ˆ˜ ์žˆ์–ด์„œ ์‹คํ–‰์‹œ๊ฐ„์ด ํ›จ์”ฌ ๋นจ๋ž๋‹ค.

let input = readLine()!.split(separator: " ").map{ Int($0)! }
let n = input[0]
let k = input[1]
var answer = 0
(0...n).forEach { hour in
    (0...59).forEach { minute in
        (0...59).forEach { second in
            if hour / 10 == k || hour % 10 == k
                || minute / 10 == k || minute % 10 == k
                || second / 10 == k || second % 10 == k {
                answer += 1
            }
        }
    }
}
print(answer)