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 ninstead 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.
In the only line of input there is an integer n (3 ≤ n ≤ 1000), the sum of numerator and denominator of the fraction.
Output two space-separated positive integers a and b, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.
3
1 2
4
1 3
12
5 7
题意:
a + b = n , a < b , a / b 为最简分数,已知n,求 a / b 最大时 a、b 的值
思路:
gcd(a,b) = 1 遍历一下就行了
#include<stdio.h>
int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}
int main()
{
int n;
scanf("%d",&n);
for(int i=n/2;;i--) if(gcd(i,n-i)==1) {printf("%d %d\n",i,n-i);break;}
return 0;
}
本文介绍了一个数学问题的解决方法,即给定整数n,找出使得分子分母之和等于n的最大且不可约简的真分数。通过遍历并利用辗转相除法判断互质的方式,最终确定满足条件的最大分数。
9351

被折叠的 条评论
为什么被折叠?



