Tuesday, June 21, 2016

11827 - Maximum GCD

Problem Linkhttps://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2927

Solution Idea : No idea needed to solve this problem!! But we will face problem while taking array elements..for this we have to use stringstream.

Solution :
#include<bits/stdc++.h>
#define ll long long

using namespace std;

ll int gcd(ll int p,ll int q)
{

    ll int gcd;

    ll int temp;
    while(q!=0){

        temp=q;
        q=p%q;
        p=temp;
    }

    gcd=p;
    return gcd;
}

ll int a[103];

int main()
{
    ll int n,m,mx=0,g=0,j=0,ans=0;
    string s;

    scanf("%lld",&n);
    getchar();

    while(n--)
    {
        getline(cin,s);
        stringstream ss(s);
        j=0;
        ans=0;
        while(ss>>a[j]) j++;

        for(int i=0;i<j;i++)
        {
            for(int k=i+1;k<j;k++)
            {
                g=gcd(a[i],a[k]);
                ans=max(ans,g);
            }

        }
        printf("%lld\n",ans);
    }

}

No comments:

Post a Comment