A. Fraction
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.
Examples
inputCopy
3
outputCopy
1 2
inputCopy
4
outputCopy
1 3
inputCopy
12
outputCopy
5 7
**题解:**本题的主要意思是给你一个大于等于3小于等于1000的数 n,然后a + b = n,(a < b)问当 a 等于多少,b等于多少 时,a / b最大。
当然是a与b越接近a/b的值越大。同时要保证gcd(a , b) = 1.
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int gcd(int a,int b)
{
if(a % b == 0)
return b;
else
return gcd(b,a % b);
}
int main()
{
int n;
cin >> n;
if(n % 2 == 0)
{
if((n/2) % 2 == 0)
printf("%d %d\n",n/2 - 1,n/2 + 1);
else
{
for(int i = n/2 - 1,j = n/2 + 1; i >= 1,j < n; i --,j ++)
{
if(gcd(i,j) == 1)
{
printf("%d %d\n",i,j);
break;
}
}
}
}
else
printf("%d %d\n",n/2,n/2 + 1);
return 0;
}