📜  找到两个数字的移位表之间的最小差

📅  最后修改于: 2021-05-06 19:03:22             🧑  作者: Mango

给定两个数字“ a”和“ b”。在给定移位x和y的情况下,在移位的无限表’a’和’b’中找到任何项之间的最小差,其中x,y> = 0。
让我们考虑a = 6和b = 16。
a的表格: 6、12、18、24、30、36、42、48
B的表:16,32,48,64,80,96,112,128 … ..
设给定的位移为x = 5且y = 2
的移位的表:11,17,23,29,35,41,47
B的移位表:18,34,50,66,82,98,114
上面两个移位表中的任意两个项之间的最小差为1(请参阅彩色项)。因此输出应为1。
强烈建议您最小化浏览器,然后自己尝试。
算法:

  1. 计算数字“ a”和“ b”的最大公约数(GCD)。设两个数字的GCD为“ g”。
  2. 计算模“ g”下位移“ x”和“ y”的绝对差“ diff”,即diff = abs(a – b)%g
  3. 所需的移位差异为“ diff”和“ g – diff”的最小值。

下面是上述算法的实现。

C++
// C++ program to find the minimum difference
// between any two terms of two tables
#include 
using namespace std;
 
// Utility function to find GCD of a and b
int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}
 
// Returns minimum difference between
// any two terms of shifted tables of 'a'
// and 'b'. 'x' is shift in table of 'a'
// and 'y' is shift in table of 'b'.
int findMinDiff(int a, int b, int x, int y)
{
    // Calculate gcd of a nd b
    int g  = gcd(a,b);
 
    // Calculate difference between x and y
    int diff = abs(x-y) % g;
 
    return min(diff, g - diff);
}
 
// Driver Code
int main()
{
    int a = 20, b = 52, x = 5, y = 7;
 
    cout << findMinDiff(a, b, x, y) << endl;
 
    return 0;
}


Java
// Java program to find the
// minimum difference between
// any two terms of two tables
import java.io.*;
 
class GFG
{
     
// Utility function to
// find GCD of a and b
static int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}
 
// Returns minimum difference
// between any two terms of
// shifted tables of 'a' and
// 'b'. 'x' is shift in table
// of 'a' and 'y' is shift in
// table of 'b'.
static int findMinDiff(int a, int b,
                       int x, int y)
{
    // Calculate gcd
    // of a nd b
    int g = gcd(a,b);
 
    // Calculate difference
    // between x and y
    int diff = Math.abs(x - y) % g;
 
    return Math.min(diff, g - diff);
}
 
// Driver Code
public static void main (String[] args)
{
    int a = 20, b = 52, x = 5, y = 7;
    System.out.println( findMinDiff(a, b, x, y));
}
}
 
// This code is contributed by ajit


Python3
# python3 program to find the minimum difference
# between any two terms of two tables
import math as mt
   
# Utility function to find GCD of a and b
def gcd(a,b):
 
    while (b != 0):
        t = b
        b = a % b
        a = t
 
    return a
  
# Returns minimum difference between
# any two terms of shifted tables of 'a'
# and 'b'. 'x' is shift in table of 'a'
# and 'y' is shift in table of 'b'.
def findMinDiff (a, b, x, y):
    # Calculate gcd of a nd b
    g  = gcd(a,b)
   
    # Calculate difference between x and y
    diff = abs(x-y) % g
   
    return min(diff, g - diff)
   
# Driver Code
a,b,x,y = 20,52,5,7
 
print(findMinDiff(a, b, x, y))
#This code is contributed by Mohit kumar 29


C#
// C# program to find the minimum difference
// between any two terms of two tables
using System;
class GFG
{
     
// Utility function to find GCD of a and b
static int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}
 
// Returns minimum difference between any
// two terms of shifted tables of 'a' and
// 'b'. 'x' is shift in table of 'a' and
// 'y' is shift in table of 'b'.
static int findMinDiff(int a, int b,
                       int x, int y)
{
    // Calculate gcd
    // of a nd b
    int g = gcd(a, b);
 
    // Calculate difference
    // between x and y
    int diff = Math.Abs(x - y) % g;
 
    return Math.Min(diff, g - diff);
}
 
// Driver Code
static void Main()
{
    int a = 20, b = 52, x = 5, y = 7;
    Console.WriteLine(findMinDiff(a, b, x, y));
}
}
 
// This code is contributed by mits


PHP


Javascript


输出 :

2

复杂度: O(log n),用于计算GCD。
这是如何运作的?
移位表后变为(假设y> x且diff = yx)
a + x,2a + x,3a + x,.. ab + x,…。和b + y,2b + y,3b + y,….,ab + y,…。
通常,差异为abs [(am + x)–(bn + y)],其中m> = 1和n> = 1
最小差异的上限是“ abs(x – y)”。我们总是可以通过放置m = b和n = a来获得这种差异。
我们如何才能将绝对差减小到“ abs(x – y)”以下?
让我们将“ abs [(am + x)–(bn + y)]”重写为abs [(x – y)–(bn – am)]
令“ a”和“ b”的GCD为“ g”。让我们考虑一下“ g”表。该表包含所有术语,例如a,2a,3a,… b,2b,3b,…以及术语(bn-am)和(am-bn)。