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 925 (Div. 3) 1931D. Divisible Pairs Solution

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

Powered by Blogger.