📜  找到具有最小绝对差的给定分数的最接近分数

📅  最后修改于: 2022-05-13 01:56:09.740000             🧑  作者: Mango

找到具有最小绝对差的给定分数的最接近分数

给定三个整数xyn,任务是找到这样的一对整数a, b (1 <= b <= n; 0 <= a)使得值|x/y – a/b|尽可能小,其中 |x|表示 x 的绝对值。

例子:

方法:迭代分母。设分母为i 。那么需要选择这样一个分子d使得|x/y – d/i|是最小的。 d = (x*i)/y是一个不错的选择。还要检查d+1 。将答案从A/B更新为d/i 时,检查x/y – d/i < x/y -A/B(B*xy*A) * (i*y) > (i*xy* d) * (B*y)。请按照以下步骤解决问题:

  • 将变量AB初始化为-1以存储答案。
  • 使用变量i迭代范围[1, N]并执行以下步骤:
    • 将变量d初始化为(i*x)/y作为最接近的可能分子。
    • 如果d大于等于0A等于-1ABS(B * x – y * A) * ABS(i * y)大于ABS(i * x – y * d) * ABS(B * y),然后将A的值设置为d ,将B的值设置为i。
    • d的值增加1。
    • 如果d大于等于0A等于-1ABS(B * x – y * A) * ABS(i * y)大于ABS(i * x – y * d) * ABS(B * y),然后将A的值设置为d ,将B的值设置为i。
  • 执行上述步骤后,打印A/B的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the absolute
// value of x
long long ABS(long long x) { return max(x, -x); }
 
// Function to find the fraction with
// minimum absolute difference
void findFraction(long long x, long long y, long long n)
{
 
    // Initialize the answer variables
    long long A = -1, B = -1;
 
    // Iterate over the range
    for (long long i = 1; i <= n; i++) {
 
        // Nearest fraction
        long long d = (i * x) / y;
 
        // x/y - d/i < x/y -A/B
        //(B*x-y*A) * (i*y) > (i*x-y*d) * (B*y)
        if (d >= 0
            && (A == -1
                || ABS(B * x - y * A) * ABS(i * y)
                       > ABS(i * x - y * d) * ABS(B * y)))
            A = d, B = i;
 
        // Check for d+1
        d++;
        if (d >= 0
            && (A == -1
                || ABS(B * x - y * A) * ABS(i * y)
                       > ABS(i * x - y * d) * ABS(B * y)))
            A = d, B = i;
    }
 
    // Print the answer
    cout << A << "/" << B << endl;
}
 
// Driver Code
int main()
{
 
    long long x = 3, y = 7, n = 6;
 
    findFraction(x, y, n);
 
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
class GFG
{
   
    // Function to find the absolute
    // value of x
    static long ABS(long x) { return Math.max(x, -x); }
 
    // Function to find the fraction with
    // minimum absolute difference
    static void findFraction(long x, long y, long n)
    {
 
        // Initialize the answer variables
        long A = -1, B = -1;
 
        // Iterate over the range
        for (long i = 1; i <= n; i++) {
 
            // Nearest fraction
            long d = (i * x) / y;
 
            // x/y - d/i < x/y -A/B
            //(B*x-y*A) * (i*y) > (i*x-y*d) * (B*y)
            if (d >= 0
                && (A == -1
                    || ABS(B * x - y * A) * ABS(i * y)
                           > ABS(i * x - y * d)
                                 * ABS(B * y)))
                A = d;
            B = i;
 
            // Check for d+1
            d++;
            if (d >= 0
                && (A == -1
                    || ABS(B * x - y * A) * ABS(i * y)
                           > ABS(i * x - y * d)
                                 * ABS(B * y)))
                A = d;
            B = i;
        }
        A--;
        B--;
       
        // Print the answer
        System.out.println(A + "/" + B);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        long x = 3;
        long y = 7;
        long n = 6;
 
        findFraction(x, y, n);
    }
}
 
// This code is contributed by lokeshpotta.


Python3
# Python3 program for the above approach
 
# Function to find the absolute
# value of x
def ABS(x):
     
    return max(x, -x)
 
# Function to find the fraction with
# minimum absolute difference
def findFraction(x, y, n):
 
    # Initialize the answer variables
    A = -1
    B = -1
 
    # Iterate over the range
    for i in range(1, n + 1):
 
        # Nearest fraction
        d = (i * x) // y
 
        # x/y - d/i < x/y -A/B
        # (B*x-y*A) * (i*y) > (i*x-y*d) * (B*y)
        if (d >= 0 and (A == -1 or
           ABS(B * x - y * A) * ABS(i * y) >
           ABS(i * x - y * d) * ABS(B * y))):
            A = d
            B = i
 
        # Check for d+1
        d += 1
        if (d >= 0 and (A == -1 or
           ABS(B * x - y * A) * ABS(i * y) >
           ABS(i * x - y * d) * ABS(B * y))):
            A = d
            B = i
 
    # Print the answer
    print(str(A) + "/" + str(B))
 
# Driver Code
if __name__ == '__main__':
 
    x = 3
    y = 7
    n = 6
 
    findFraction(x, y, n)
 
# This code is contributed by mohit kumar 29


C#
// C# code for the above approach
using System;
 
public class GFG{
 
    // Function to find the absolute
    // value of x
    static long ABS(long x) { return Math.Max(x, -x); }
 
    // Function to find the fraction with
    // minimum absolute difference
    static void findFraction(long x, long y, long n)
    {
 
        // Initialize the answer variables
        long A = -1, B = -1;
 
        // Iterate over the range
        for (long i = 1; i <= n; i++) {
 
            // Nearest fraction
            long d = (i * x) / y;
 
            // x/y - d/i < x/y -A/B
            //(B*x-y*A) * (i*y) > (i*x-y*d) * (B*y)
            if (d >= 0
                && (A == -1
                    || ABS(B * x - y * A) * ABS(i * y)
                           > ABS(i * x - y * d)
                                 * ABS(B * y)))
                A = d;
            B = i;
 
            // Check for d+1
            d++;
            if (d >= 0
                && (A == -1
                    || ABS(B * x - y * A) * ABS(i * y)
                           > ABS(i * x - y * d)
                                 * ABS(B * y)))
                A = d;
            B = i;
        }
        A--;
        B--;
       
        // Print the answer
        Console.Write(A + "/" + B);
    }
 
    // Driver Code
    static public void Main (){
 
        long x = 3;
        long y = 7;
        long n = 6;
 
        findFraction(x, y, n);
    }
}
 
// This code is contributed by shubhamsingh10.


Javascript


输出
2/5

时间复杂度: O(N)
辅助空间: O(1)