SDNU_ACM_ICPC_2020_Winter_Practice_1st A

题目

Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction is called proper iff its numerator is smaller than its denominator (a < b) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have positive common divisors except 1).

During his free time, Petya thinks about proper irreducible fractions and converts them to decimals using the calculator. One day he mistakenly pressed addition button ( + ) instead of division button (÷) and got sum of numerator and denominator that was equal to n instead of the expected decimal notation.

Petya wanted to restore the original fraction, but soon he realized that it might not be done uniquely. That’s why he decided to determine maximum possible proper irreducible fraction such that sum of its numerator and denominator equals n. Help Petya deal with this problem.

Input
In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.

Output
Output two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.
SDNU_ACM_ICPC_2020_Winter_Practice_1st A
大意:
将输入的数拆成两个数a,b。a,b互质,求出使得a/b最大的情况。

AC代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
int gcd(int a,int b)
{
    while(b^=a^=b^=a%=b);return a;
}
int main()
{
    int a,b,sum;
    cin>>sum;
    a=sum/2;
    b=sum-a;
    for(int i=a;i>=1;i--)
    {
        if(gcd(a,b)==1){cout<<a<<" "<<b;break;}
        a--;
        b++;
    }
    return 0;
}
SDNU_ACM_ICPC_2020_Winter_Practice_1st ASDNU_ACM_ICPC_2020_Winter_Practice_1st A 夷则九 发布了16 篇原创文章 · 获赞 0 · 访问量 167 私信 关注
上一篇:A+B for Input-Output Practice (I)


下一篇:用Git管理项目进行版本控制