UVa 11827 - Maximum GCD Solution

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
vector<int> f;
int gcd(int a,int b){
    if(!a)
        return b;
    return gcd(b%a,a);
}
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        ws(cin);
        getline(cin,s);
        stringstream ss(s);
        int x;
        vector<int> a;
        while(ss>>x)
            a.push_back(x);
        int ans=0;
        for(int i=0;i<a.size()-1;i++)
            for(int j=i+1;j<a.size();j++)
                ans=max(ans,gcd(a[i],a[j]));
        cout<<ans<<endl;
    }
}

Comments