📌  相关文章
📜  给定的3个数字应除以的最大数,以使它们剩下相同的余数

📅  最后修改于: 2021-04-27 23:37:35             🧑  作者: Mango

给定三个数字,我们的任务是找到最大的数字,当给定的三个数字相除时,得出相同的余数。可以假设所有给定的数字都是按递增顺序给出的。

例子:

Input : a = 62, b = 132, c = 237 
Output : 35
35 leads to same remainder 27 when divides 
62, 132 and 237.
       
Input : a = 74, b = 272, c = 584
Output : 6

这个想法是基于这样一个事实,即如果一个数字用a,b和c保留相同的余数,那么它将除以它们的差。让我们理解假设x是我们的结果。设a = x * d1 + r ,其中r是a除以x时的余数。类似地,我们可以写成b = x * d2 + rb = x * d3 + r 。因此,这里的逻辑是,我们首先找到所有三对的差异,然后,我们找到差异的最大公约数以最大程度地提高结果。

以下是上述想法的实现。

C++
// C++ program to find the largest numbers that
// leads to same remainder when divides given
// three sorted numbers
#include 
using namespace std;
 
//__gcd function
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// function return number which divides these
// three number and leaves same remainder .
int sameRemainder(int a, int b, int c)
{
    // We find the differences of all three  pairs
    int a1 = (b - a), b1 = (c - b), c1 = (c - a);
 
    // Return GCD of three differences.
    return gcd(a1, gcd(b1, c1));
}
 
// driver program
int main()
{
    int a = 62, b = 132, c = 237;
    cout << sameRemainder(a, b, c) << endl;
    return 0;
}


Java
// Java program to find the largest
// numbers that leads to same
// remainder when divides given
// three sorted numbers
 
class GFG {
     
//__gcd function
static int gcd(int a, int b)
{
    if (a == 0)
    return b;
    return gcd(b % a, a);
}
 
// function return number which divides these
// three number and leaves same remainder .
static int sameRemainder(int a, int b, int c)
{
    // We find the differences of all three pairs
    int a1 = (b - a), b1 = (c - b), c1 = (c - a);
 
    // Return GCD of three differences.
    return gcd(a1, gcd(b1, c1));
}
 
// Driver code
public static void main(String[] args)
{
    int a = 62, b = 132, c = 237;
    System.out.println(sameRemainder(a, b, c));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find
# the largest numbers that
# leads to same remainder
# when divides given
# three sorted numbers
 
#__gcd function
def gcd(a,b):
 
    if (a == 0):
        return b
    return gcd(b % a, a)
  
# function return number
# which divides these
# three number and leaves
# same remainder .
def sameRemainder(a,b,c):
 
    # We find the differences
    # of all three  pairs
    a1 = (b - a)
    b1 = (c - b)
    c1 = (c - a)
  
    # Return GCD of three differences.
    return gcd(a1, gcd(b1, c1))
 
# Driver program
 
a = 62
b = 132
c = 237
print(sameRemainder(a, b, c))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find the largest
// numbers that leads to same
// remainder when divides given
// three sorted numbers
using System;
 
class GFG {
     
// gcd function
static int gcd(int a, int b)
{
    if (a == 0)
    return b;
    return gcd(b % a, a);
}
 
// function return number which divides
// these three number and leaves same
// remainder .
static int sameRemainder(int a, int b, int c)
{
    // We find the differences of all three pairs
    int a1 = (b - a), b1 = (c - b), c1 = (c - a);
 
    // Return GCD of three differences.
    return gcd(a1, gcd(b1, c1));
}
 
// Driver code
public static void Main()
{
    int a = 62, b = 132, c = 237;
    Console.WriteLine(sameRemainder(a, b, c));
}
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

35

时间复杂度: O(log N)

辅助空间: O(1)