Thursday, April 23, 2015

UVa 11827 - Maximum GCD

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int gcd(int a, int b) {
	while (b) {
		int t = b;
		b = a % b;
		a = t;
	}
	return a;
}

int main() {
	int cases;
	cin >> cases;
	string line;
	getline(cin, line);
	for (; cases; cases--) {
		getline(cin, line);
		istringstream istream(line);
		int a[100], n = 0;
		while (istream >> a[n])
			n++;

		int greatest_gcd = 1;
		for (int i = 1; i < n; i++)
			for (int j = 0; j < i; j++) {
				int g = gcd(a[i], a[j]);
				if (g > greatest_gcd)
					greatest_gcd = g;
			}
		cout << greatest_gcd << endl;
	}
	return 0;
}

No comments:

Post a Comment