📌  相关文章
📜  分子和分母之和等于给定数的最大固有分数

📅  最后修改于: 2021-04-24 03:31:13             🧑  作者: Mango

我们得到一个数字N。找到最大适当的分数a / b,使a + b =N。以下是分数的约束。

  1. 如果a
  2. 可以存在多个分子和分母之和等于给定数的适当小数。主要任务是找到具有最大浮点值的分数。

例子:

Input : N = 3
Output : 1 2

Input : N = 12
Output : 5 7
Explanation: In the second example N = 12
Possible a and b's are: 1 11 
                        5 7
But clearly 5/7 (=0.71..) is greater than 
1/11 (=0.09..). Hence answer for N = 12 
is 5 7.   

该问题的解决方案比算法更直观。
请仔细考虑以下几点,以了解稍后介绍的公式:

  • 如果分子尽可能大且分母尽可能小,则小数具有最大值。
  • 这里的约束是分子不能大于分母的事实,它们的总和应等于N。

牢记这两点,我们可以得出以下事实:该问题的答案将是ceil(n / 2)-1和floor(n / 2)+1。

现在,此解决方案将始终适用于奇数N和所有(N / 2)为偶数的偶数N。这是由于以下事实:这两种情况将始终使用上述公式生成互质数。
现在考虑以下示例:
N = 10
ceil(10/2)-1 = 4
楼(10/2)+1 = 6
显然,4和6是错误的答案,因为它们不是互质。正确答案是3和7。
因此,对于具有奇数(N / 2)的偶数N,公式变为ceil(n / 2)-2和floor(n / 2)+2。

C++
// CPP program to find the largest fraction
// a/b such that a+b is equal to given number
// and a < b.
#include 
#include 
using namespace std;
  
void solve(int n)
{
    // Calculate N/2;
    float a = (float)n / 2;
  
    // Check if N is odd or even
    if (n % 2 != 0) 
  
        // If N is odd answer will be 
        // ceil(n/2)-1 and floor(n/2)+1
        cout << ceil(a) - 1 << " "
             << floor(a) + 1 << endl;
    else {
  
        // If N is even check if N/2 i.e a 
        // is even or odd
        if ((int)a % 2 == 0) {
  
            // If N/2 is even apply the
            // previous formula
            cout << ceil(a) - 1 << " " 
                 << floor(a) + 1 << endl;
        } 
  
        else {
          
            // If N/2 is odd answer will be 
            // ceil(N/2)-2 and floor(N/2)+2
            cout << ceil(a) - 2 << " " 
                 << floor(a) + 2 << endl;
        }
    }
}
  
// driver function
int main()
{
    int n = 34;
    solve(n);
    return 0;
}


Java
// Java program to find the 
// largest fraction a/b 
// such that a+b is equal 
// to given number and a < b.
  
class GFG
{
public static void solve(int n)
{
    // Calculate N/2;
    double a = n / 2;
      
    // Check if N is
    // odd or even
    if (n % 2 != 0) 
    {
        // If N is odd answer 
        // will be ceil(n/2)-1 
        // and floor(n/2)+1
        System.out.println((Math.ceil(a) - 1) + 
                         " " + (Math.floor(a) + 1));
    }
    else
    {
  
        // If N is even check
        // if N/2 i.e a 
        // is even or odd
        if ((int)(a) % 2 == 0) 
        {
  
            // If N/2 is even apply 
            // the previous formula
            System.out.println((Math.ceil(a) - 1) + 
                             " " + (Math.floor(a) + 1));
        } 
  
        else
        {
            // If N/2 is odd answer 
            // will be ceil(N/2)-2 
            // and floor(N/2)+2
            System.out.println((Math.ceil(a) - 2) + 
                             " " + (Math.floor(a) + 2));
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
    int n = 34;
    solve(n);
}
}
  
// This code is contributed
// by mits


Python3
# Python3 program to find 
# the largest fraction a/b 
# such that a+b is equal to
# given number and a < b.
import math
  
def solve(n):
      
    # Calculate N/2;
    a = float(n / 2);
  
    # Check if N is odd or even
    if (n % 2 != 0): 
  
        # If N is odd answer 
        # will be ceil(n/2)-1 
        # and floor(n/2)+1
        print((math.ceil(a) - 1), 
              (math.floor(a) + 1));
    else:
  
        # If N is even check if N/2
        # i.e a is even or odd
        if (a % 2 == 0):
  
            # If N/2 is even apply
            # the previous formula
            print((math.ceil(a) - 1),
                  (math.floor(a) + 1));
          
        else:
              
            # If N/2 is odd answer
            # will be ceil(N/2)-2 
            # and floor(N/2)+2
            print((math.ceil(a) - 2),
                  (math.floor(a) + 2));
  
# Driver Code
n = 34;
solve(n);
      
# This code is contributed by mits


C#
// C# program to find the 
// largest fraction a/b 
// such that a+b is equal 
// to given number and a < b.
using System;
class GFG
{
public static void solve(int n)
{
    // Calculate N/2;
    double a = n / 2;
      
    // Check if N is
    // odd or even
    if (n % 2 != 0) 
    {
        // If N is odd answer 
        // will be ceil(n/2)-1 
        // and floor(n/2)+1
        Console.WriteLine((Math.Ceiling(a) - 1) + 
                           " " + (Math.Floor(a) + 1));
    }
    else 
    {
  
        // If N is even check
        // if N/2 i.e a 
        // is even or odd
        if ((int)(a) % 2 == 0) 
        {
  
            // If N/2 is even apply 
            // the previous formula
            Console.WriteLine((Math.Ceiling(a) - 1) + 
                               " " + (Math.Floor(a) + 1));
        } 
  
        else 
        {
            // If N/2 is odd answer 
            // will be ceil(N/2)-2 
            // and floor(N/2)+2
            Console.WriteLine((Math.Ceiling(a) - 2) + 
                               " " + (Math.Floor(a) + 2));
        }
    }
}
  
// Driver code
public static void Main()
{
    int n = 34;
    solve(n);
}
}
  
// This code is contributed
// by mits


PHP


输出:

15 19