๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/9613
๋ด๊ฐ ํผ ํ์ด
- ์ฌ๊ท GCD ํจ์ ์์ฑ (๊ธฐ์ตํ์!) -> ์ต๋๊ณต์ฝ์๊ตฌํ ๋ ์ฌ์ฉ (์ ํด๋ฆฌ๋ ํธ์ ๋ฒ)
- input์ ๋ด๊ฒจ์๋ ์๋ค์ GCD๋ฅผ ๊ตฌํด์ฃผ๊ณ ๋ํด์ ์ถ๋ ฅํด์ค
let n = Int(readLine()!)!
func gcd(_ m: Int, _ n: Int) -> Int {
if n == 0 {
return m
} else {
return gcd(n, m%n)
}
}
for _ in 0..<n {
let input = readLine()!.split(separator: " ").map { Int(String($0))! }
var result: [Int] = []
for i in 1..<input.count - 1 {
for j in i+1..<input.count {
result.append(gcd(input[i], input[j]))
}
}
print(result.reduce(0, +))
}
๋ฐ์ํ
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] (Swift) 1212๋ฒ - 8์ง์ 2์ง์ (0) | 2022.02.20 |
---|---|
[๋ฐฑ์ค] (Swift) 1737๋ฒ - 2์ง์ 8์ง์ (0) | 2022.02.20 |
[๋ฐฑ์ค] (Swift) 17087๋ฒ - ์จ๋ฐ๊ผญ์ง6 (๋ฌธ์ ํ์ด ์ค๋ช , GCD์๊ณ ๋ฆฌ์ฆ_์ค์ํํธ) (0) | 2022.02.19 |
[๋ฐฑ์ค] (Swift) 11656๋ฒ - ์ ๋ฏธ์ฌ ๋ฐฐ์ด (0) | 2022.02.19 |
[๋ฐฑ์ค] (Swift) 10824๋ฒ - ๋ค ์ (0) | 2022.02.18 |