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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 2609๋ฒˆ - ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

๊ฐ์ž ๐Ÿฅ” 2022. 12. 25. 17:47
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/2609

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์†Œ์ˆ˜๋ฅผ ์ „๋ถ€ ๊ตฌํ•˜๊ณ , ์†Œ์ˆ˜๋กœ ๋‹ค ๋‚˜๋ˆ ์ฃผ๋ฉด์„œ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๋จผ์ € ๊ตฌํ•ด์•ผํ•˜๋‚˜? ์ƒ๊ฐํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ ๊ฐ™์•˜๊ณ , ํšจ์œจ์ ์ด์ง€ ๋ชปํ•˜๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์ƒ๊ฐ๋‚œ๊ฒŒ ์˜ˆ์ „์— ํ•œ๋ฒˆ ์ ‘ํ•ด๋ดค๋˜ gcd๋ผ๊ณ  ๋ถˆ๋ ธ๋˜ ๋ฌด์Šจ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์ƒ๊ฐ๋‚ฌ๋‹ค. ์ƒ๊ฐ๋‚œ ๊น€์— ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๋ ค๊ณ  ํฌ์ŠคํŒ…์„ ์ž‘์„ฑํ–ˆ๋‹ค.

https://didu-story.tistory.com/357

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] (swift) ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ• (feat. ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ ๊ตฌํ•˜๊ธฐ)

์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜, ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด์„œ๋„, ๊ณ„์† for๋ฌธ์„ ๋Œ๋ฆฌ๊ณ ์žˆ๊ณ ,,, ์‹œ๊ฐ„๋‚ญ๋น„๋ฅผ ํ•  ๋•Œ๊ฐ€ ๋งŽ๋‹ค. ์Šค์Šค๋กœ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ ์–ด๋‘๋Š” ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•!

didu-story.tistory.com

์ด ๊ธ€์—์„œ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜์™€ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‚˜์™€์žˆ๊ณ , ์ด ๋ฐฉ๋ฒ•์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

๐ŸŸ  ์ •๋‹ต ์ฝ”๋“œ

private func solution() {
    var nm = readLine()!.split(separator: " ").map { Int($0)! }
    let gcd = gcd(nm[0], nm[1])
    let lcd = (nm[0] * nm[1]) / gcd
    print(gcd)
    print(lcd)
}

private func gcd(_ n: Int, _ m: Int) -> Int {
    return m == 0 ? n : gcd(m, n%m)
}

solution()
๋ฐ˜์‘ํ˜•