Published 2020. 12. 29. 05:24
 

9613번: GCD 합

첫째 줄에 테스트 케이스의 개수 t (1 ≤ t ≤ 100)이 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있다. 각 테스트 케이스는 수의 개수 n (1 < n ≤ 100)가 주어지고, 다음에는 n개의 수가 주어진

www.acmicpc.net

#include <iostream>
#include <vector>
using namespace std;

int gcd(int a, int b) {
	if (b == 0) {
		return a;
	}
	return gcd(b, a % b);
}

auto main()->int {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

	unsigned int t = 0;
	cin >> t;
	while (t--) {
		vector<unsigned int> nums;
		unsigned int n;
		cin >> n;
		nums.reserve(n);
		for (unsigned int i = 0; i < n; i++) {
			unsigned int temp;
			cin >> temp;
			nums.emplace_back(temp);
		}
		long long ans = 0;
		for (unsigned int i = 0; i < nums.size(); i++) {
			for (unsigned int j = i + 1; j < nums.size(); j++) {
				ans += gcd(nums[i], nums[j]);
			}
		}
		cout << ans << '\n';
	}
}

'Problem set' 카테고리의 다른 글

[백준] 10799: 쇠막대기  (0) 2020.12.29
[백준] 10430: 나머지  (0) 2020.12.29
[백준] 9095: 1, 2, 3 더하기  (0) 2020.12.29
[백준] 9093: 단어 뒤집기  (0) 2020.12.29
[백준] 9012: 괄호  (0) 2020.12.29
복사했습니다!