UVA 11827 Maximum GCD Solution

Solve in C++:

///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh

#include<bits/stdc++.h>
using namespace std;
int fngcd(int p,int q)
{
    if(q==0)
        return p;
    else
        return fngcd(q,p%q);
}
int main()
{
    int i,j,ar[10000],n,gcd;
    cin>>n;
    getchar();
    string  s;
    while(n--)
    {
        getline(cin,s);
        stringstream ss(s);
        int k=0;
        while(ss>>ar[k])
            ++k;
        int gcd=0;
        for(i=0; i<k; i++)
        {
            for(j=i+1; j<k; j++)
            {
                gcd=max(gcd,fngcd(ar[i],ar[j]));

            }

        }
        cout<<gcd<<endl;

    }
    return 0;
}


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.