Monday, November 6, 2017

A. Fraction Codeforces Problem Solution

http://codeforces.com/problemset/problem/854/Av

#include<bits/stdc++.h>

using namespace std;
int gcd(int a,int b)
{
     if (a == 0 || b == 0)
       return 0;

    // base case
    if (a == b)
        return a;

    // a is greater
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}
int main()

{

    int a,b,i;
    cin>>a;
    for(i=a/2;i>=1;i--)
    {
        b=gcd(i,a-i);
        if(b==1)
        {
            break;
        }
    }
    cout<<min(i,a-i)<<" "<<max(i,a-i)<<endl;
    return 0;
}

No comments:

Post a Comment