시배's Android
Algorithm | Kotlin 백준 9613 GCD 합 본문
9613번: GCD 합
첫째 줄에 테스트 케이스의 개수 t (1 ≤ t ≤ 100)이 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있다. 각 테스트 케이스는 수의 개수 n (1 < n ≤ 100)가 주어지고, 다음에는 n개의 수가 주어진
www.acmicpc.net
private fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
fun gcd(a: Long, b: Long): Long = if (b == 0L) a else gcd(b, a % b)
repeat(readLine()!!.toInt()) {
val a = readLine()!!.split(" ").map { it.toLong() }
println((1 until a.size).map { i -> (1 until i).map { j -> gcd(a[i], a[j]) }.sum() }.sum())
}
}
'Algorithm' 카테고리의 다른 글
Algorithm | 백준 Kotlin 15664 N과 M (10) (0) | 2023.09.16 |
---|---|
Algorithm | 백준 Kotlin 2776 암기왕 (0) | 2023.09.16 |
Algorithm | Kotlin 백준 16918 봄버맨 (0) | 2023.09.11 |
Algorithm | Kotlin 백준 1059 좋은 구간 (0) | 2023.09.11 |
Algorithm | Kotlin 백준 17144 미세먼지 안녕! (0) | 2023.09.04 |