Bibi's DevLog ๐ค๐
[๋ฐฑ์ค 18312] ์๊ฐ (Swift) ๋ณธ๋ฌธ
ํ๋ก๊ทธ๋๋ฐ/์๊ณ ๋ฆฌ์ฆ ํ์ด Swift
[๋ฐฑ์ค 18312] ์๊ฐ (Swift)
๋น๋น bibi 2022. 12. 13. 15:20์ด์ฝํ (์ด๊ฒ์ด ์ฝ๋ฉ ํ ์คํธ๋ค) ๊ตฌํ ์์ ์ค ํ๋. (113ํ์ด์ง)
- 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)
'ํ๋ก๊ทธ๋๋ฐ > ์๊ณ ๋ฆฌ์ฆ ํ์ด Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 10539] ์๋น์ด์ ์์ด (Swift) (0) | 2023.01.11 |
---|---|
[๋ฐฑ์ค 10101] ์ผ๊ฐํ ์ธ์ฐ๊ธฐ(Swift) (0) | 2022.12.14 |
[์ด์ฝํ ] 4.๊ตฌํ - 2. ์์ค์ ๋์ดํธ (Swift) (0) | 2022.12.13 |
[๋ฐฑ์ค 9501] ๊ฟ์ ์ฐ์ฃผ์ฌํ (Swift) (0) | 2022.12.12 |
[๋ฐฑ์ค 9469, 14924] ํฐ ๋ ธ์ด๋ง, ํฐ ๋ ธ์ด๋ง๊ณผ ํ๋ฆฌ (Swift) (0) | 2022.12.09 |