📌  相关文章
📜  用 K 最小化给定三个数字的任意倍数之间的差异

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

用 K 最小化给定三个数字的任意倍数之间的差异

给定 4 个整数ABCK ,任务是找到 A、B 和 C 的任意倍数与 K 之间的最小

例子:

天真的方法:可以通过使用 for 循环来解决该任务,通过跟踪仅大于K最接近的倍数来获取 A、B 和 C 的下一个倍数按照以下步骤解决问题:

  • 初始化 3 个布尔变量 fa、fb 和 fc 来表示 A、B 和 C 的倍数是否大于K
  • 开始将 A、B 和 C 与初始化为 1 的cur相乘
  • 一旦三个数字之一变得大于K ,相应地存储最接近的倍数与 K 之间的差

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the difference between
// closest multiple of A, B, C with K
void solve(int A, int B, int C, int K)
{
 
    // Stores the minimum difference
    int ans = INT_MAX;
 
    // Check whether multiples of A, B and C becomes
    // greater than K
    bool fa = false, fb = false, fc = false;
 
    // Start multiplication from 1
    int cur = 1;
    while (1) {
 
        // Finding multiples
        A *= cur;
        B *= cur;
        C *= cur;
 
        // All the multiples becomes
        // greater than K
        if (fa && fb && fb)
            break;
 
        // Multiple of A
        if (!fa) {
 
            // Valid multiple
            if (A >= K) {
 
                // Minimize ans
                ans = min(ans, A - K);
                fa = true;
            }
        }
 
        // Multiple of B
        if (!fb) {
 
            // Valid multiple
            if (B >= K) {
 
                // Minimize ans
                ans = min(ans, B - K);
                fb = true;
            }
        }
 
        // Multiple of C
        if (!fc) {
 
            // Valid multiple
            if (C >= K) {
 
                // Minimize ans
                ans = min(ans, C - K);
                fc = true;
            }
        }
 
        cur++;
    }
 
    // Resultant answer
    cout << ans << endl;
}
 
// Driver Code
int main()
{
    int A = 6, B = 10, C = 9, K = 2;
    solve(A, B, C, K);
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the difference between
// closest multiple of A, B, C with K
static void solve(int A, int B, int C, int K)
{
 
    // Stores the minimum difference
    int ans = Integer.MAX_VALUE;
 
    // Check whether multiples of A, B and C becomes
    // greater than K
    boolean fa = false, fb = false, fc = false;
 
    // Start multiplication from 1
    int cur = 1;
    while (true) {
 
        // Finding multiples
        A *= cur;
        B *= cur;
        C *= cur;
 
        // All the multiples becomes
        // greater than K
        if (fa && fb && fb)
            break;
 
        // Multiple of A
        if (!fa) {
 
            // Valid multiple
            if (A >= K) {
 
                // Minimize ans
                ans = Math.min(ans, A - K);
                fa = true;
            }
        }
 
        // Multiple of B
        if (!fb) {
 
            // Valid multiple
            if (B >= K) {
 
                // Minimize ans
                ans = Math.min(ans, B - K);
                fb = true;
            }
        }
 
        // Multiple of C
        if (!fc) {
 
            // Valid multiple
            if (C >= K) {
 
                // Minimize ans
                ans = Math.min(ans, C - K);
                fc = true;
            }
        }
 
        cur++;
    }
 
    // Resultant answer
    System.out.print(ans +"\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int A = 6, B = 10, C = 9, K = 2;
    solve(A, B, C, K);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python Program to implement
# the above approach
 
# Function to find the difference between
# closest multiple of A, B, C with K
def solve(A, B, C, K):
 
    # Stores the minimum difference
    ans = 10 ** 9
 
    # Check whether multiples of A, B and C becomes
    # greater than K
    fa = False
    fb = False
    fc = False
 
    # Start multiplication from 1
    cur = 1
    while (1):
 
        # Finding multiples
        A *= cur
        B *= cur
        C *= cur
 
        # All the multiples becomes
        # greater than K
        if (fa and fb and fb):
            break
 
        # Multiple of A
        if (not fa):
 
            # Valid multiple
            if (A >= K):
 
                # Minimize ans
                ans = min(ans, A - K)
                fa = True
 
        # Multiple of B
        if (not fb):
 
            # Valid multiple
            if (B >= K):
 
                # Minimize ans
                ans = min(ans, B - K)
                fb = True
 
        # Multiple of C
        if (not fc):
 
            # Valid multiple
            if (C >= K):
 
                # Minimize ans
                ans = min(ans, C - K)
                fc = True
 
        cur += 1
 
    # Resultant answer
    print(ans)
 
# Driver Code
A = 6
B = 10
C = 9
K = 2
solve(A, B, C, K)
 
 # This code is contributed by saurabh_jaiswal.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG {
 
// Function to find the difference between
// closest multiple of A, B, C with K
static void solve(int A, int B, int C, int K)
{
 
    // Stores the minimum difference
    int ans = Int32.MaxValue;
 
    // Check whether multiples of A, B and C becomes
    // greater than K
    bool fa = false, fb = false, fc = false;
 
    // Start multiplication from 1
    int cur = 1;
    while (true) {
 
        // Finding multiples
        A *= cur;
        B *= cur;
        C *= cur;
 
        // All the multiples becomes
        // greater than K
        if (fa && fb && fb)
            break;
 
        // Multiple of A
        if (!fa) {
 
            // Valid multiple
            if (A >= K) {
 
                // Minimize ans
                ans = Math.Min(ans, A - K);
                fa = true;
            }
        }
 
        // Multiple of B
        if (!fb) {
 
            // Valid multiple
            if (B >= K) {
 
                // Minimize ans
                ans = Math.Min(ans, B - K);
                fb = true;
            }
        }
 
        // Multiple of C
        if (!fc) {
 
            // Valid multiple
            if (C >= K) {
 
                // Minimize ans
                ans = Math.Min(ans, C - K);
                fc = true;
            }
        }
 
        cur++;
    }
 
    // Resultant answer
    Console.Write(ans +"\n");
}
 
    // Driver Code
    public static void Main()
    {
        int A = 6, B = 10, C = 9, K = 2;
        solve(A, B, C, K);
    }
}
 
// This code is contributed by sanjoy_62.


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the difference between
// closest multiple of A, B, C with K
void solve(int A, int B, int C, int K)
{
    // Stores the multiples of A, B, C
    // just greater than K
    int fa, fb, fc;
 
    // Multiple of A
    if ((K % A) != 0) {
 
        fa = ((K / A) + 1) * A;
    }
    else {
        fa = (K / A) * A;
    }
 
    // Multiple of B
    if ((K % B) != 0) {
 
        fb = ((K / B) + 1) * B;
    }
    else {
        fb = (K / B) * B;
    }
 
    // Multiple of C
    if ((K % C) != 0) {
 
        fc = ((K / C) + 1) * C;
    }
    else {
        fc = (K / C) * C;
    }
 
    // Store the resultant answer
    int ans;
    ans = min(fa - K, min(fc - K, fb - K));
    cout << ans << endl;
}
 
// Drive code
int main()
{
    int A = 6, B = 10, C = 9, K = 2;
    solve(A, B, C, K);
    return 0;
}


Java
// Java program for the above approach
public class GFG {
     
    // Function to find the difference between
    // closest multiple of A, B, C with K
    static void solve(int A, int B, int C, int K)
    {
       
        // Stores the multiples of A, B, C
        // just greater than K
        int fa, fb, fc;
     
        // Multiple of A
        if ((K % A) != 0) {
     
            fa = ((K / A) + 1) * A;
        }
        else {
            fa = (K / A) * A;
        }
     
        // Multiple of B
        if ((K % B) != 0) {
     
            fb = ((K / B) + 1) * B;
        }
        else {
            fb = (K / B) * B;
        }
     
        // Multiple of C
        if ((K % C) != 0) {
     
            fc = ((K / C) + 1) * C;
        }
        else {
            fc = (K / C) * C;
        }
     
        // Store the resultant answer
        int ans;
        ans = Math.min(fa - K, Math.min(fc - K, fb - K));
        System.out.println(ans);
    }
 
    // Drive code
    public static void main (String[] args)
    {
        int A = 6, B = 10, C = 9, K = 2;
        solve(A, B, C, K);
    }
 
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
 
# Function to find the difference between
# closest multiple of A, B, C with K
def solve( A, B, C, K) :
 
    # Stores the multiples of A, B, C
    # just greater than K
 
    # Multiple of A
    if ((K % A) != 0) :
        fa = ((K // A) + 1) * A;
    else :
        fa = (K // A) * A;
 
    # Multiple of B
    if ((K % B) != 0) :
 
        fb = ((K // B) + 1) * B;
     
    else :
        fb = (K // B) * B;
 
    # Multiple of C
    if ((K % C) != 0) :
 
        fc = ((K // C) + 1) * C;
    else :
        fc = (K // C) * C;
 
    # Store the resultant answer
    ans = min(fa - K, min(fc - K, fb - K));
     
    print(ans);
 
# Drive code
if __name__ == "__main__" :
 
    A = 6; B = 10; C = 9; K = 2;
    solve(A, B, C, K);
 
    # This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
public class GFG {
 
    // Function to find the difference between
    // closest multiple of A, B, C with K
    static void solve(int A, int B, int C, int K)
    {
 
        // Stores the multiples of A, B, C
        // just greater than K
        int fa, fb, fc;
 
        // Multiple of A
        if ((K % A) != 0) {
 
            fa = ((K / A) + 1) * A;
        }
        else {
            fa = (K / A) * A;
        }
 
        // Multiple of B
        if ((K % B) != 0) {
 
            fb = ((K / B) + 1) * B;
        }
        else {
            fb = (K / B) * B;
        }
 
        // Multiple of C
        if ((K % C) != 0) {
 
            fc = ((K / C) + 1) * C;
        }
        else {
            fc = (K / C) * C;
        }
 
        // Store the resultant answer
        int ans;
        ans = Math.Min(fa - K, Math.Min(fc - K, fb - K));
        Console.WriteLine(ans);
    }
 
    // Drive code
    public static void Main(string[] args)
    {
        int A = 6, B = 10, C = 9, K = 2;
        solve(A, B, C, K);
    }
}
 
// This code is contributed by ukasp.


Javascript



输出
4

时间复杂度:O(min(K/A, K/B, K/C))
辅助空间:O(1)

有效方法:上述方法可以概括为一个公式: min(⌈K/A⌉*A, ⌈K/B⌉*B, ⌈K/C⌉*C) - K

  • ⌈K/A⌉*A给出刚刚大于KA的最近倍数
  • ⌈K/B⌉*B给出B的最近倍数,刚好大于K
  • ⌈K/C⌉*C给出C的最近倍数,刚好大于K
  • 所有这些中的最小值与K差异给出了预期的结果

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the difference between
// closest multiple of A, B, C with K
void solve(int A, int B, int C, int K)
{
    // Stores the multiples of A, B, C
    // just greater than K
    int fa, fb, fc;
 
    // Multiple of A
    if ((K % A) != 0) {
 
        fa = ((K / A) + 1) * A;
    }
    else {
        fa = (K / A) * A;
    }
 
    // Multiple of B
    if ((K % B) != 0) {
 
        fb = ((K / B) + 1) * B;
    }
    else {
        fb = (K / B) * B;
    }
 
    // Multiple of C
    if ((K % C) != 0) {
 
        fc = ((K / C) + 1) * C;
    }
    else {
        fc = (K / C) * C;
    }
 
    // Store the resultant answer
    int ans;
    ans = min(fa - K, min(fc - K, fb - K));
    cout << ans << endl;
}
 
// Drive code
int main()
{
    int A = 6, B = 10, C = 9, K = 2;
    solve(A, B, C, K);
    return 0;
}

Java

// Java program for the above approach
public class GFG {
     
    // Function to find the difference between
    // closest multiple of A, B, C with K
    static void solve(int A, int B, int C, int K)
    {
       
        // Stores the multiples of A, B, C
        // just greater than K
        int fa, fb, fc;
     
        // Multiple of A
        if ((K % A) != 0) {
     
            fa = ((K / A) + 1) * A;
        }
        else {
            fa = (K / A) * A;
        }
     
        // Multiple of B
        if ((K % B) != 0) {
     
            fb = ((K / B) + 1) * B;
        }
        else {
            fb = (K / B) * B;
        }
     
        // Multiple of C
        if ((K % C) != 0) {
     
            fc = ((K / C) + 1) * C;
        }
        else {
            fc = (K / C) * C;
        }
     
        // Store the resultant answer
        int ans;
        ans = Math.min(fa - K, Math.min(fc - K, fb - K));
        System.out.println(ans);
    }
 
    // Drive code
    public static void main (String[] args)
    {
        int A = 6, B = 10, C = 9, K = 2;
        solve(A, B, C, K);
    }
 
}
 
// This code is contributed by AnkThon

Python3

# Python3 program for the above approach
 
# Function to find the difference between
# closest multiple of A, B, C with K
def solve( A, B, C, K) :
 
    # Stores the multiples of A, B, C
    # just greater than K
 
    # Multiple of A
    if ((K % A) != 0) :
        fa = ((K // A) + 1) * A;
    else :
        fa = (K // A) * A;
 
    # Multiple of B
    if ((K % B) != 0) :
 
        fb = ((K // B) + 1) * B;
     
    else :
        fb = (K // B) * B;
 
    # Multiple of C
    if ((K % C) != 0) :
 
        fc = ((K // C) + 1) * C;
    else :
        fc = (K // C) * C;
 
    # Store the resultant answer
    ans = min(fa - K, min(fc - K, fb - K));
     
    print(ans);
 
# Drive code
if __name__ == "__main__" :
 
    A = 6; B = 10; C = 9; K = 2;
    solve(A, B, C, K);
 
    # This code is contributed by AnkThon

C#

// C# program for the above approach
using System;
public class GFG {
 
    // Function to find the difference between
    // closest multiple of A, B, C with K
    static void solve(int A, int B, int C, int K)
    {
 
        // Stores the multiples of A, B, C
        // just greater than K
        int fa, fb, fc;
 
        // Multiple of A
        if ((K % A) != 0) {
 
            fa = ((K / A) + 1) * A;
        }
        else {
            fa = (K / A) * A;
        }
 
        // Multiple of B
        if ((K % B) != 0) {
 
            fb = ((K / B) + 1) * B;
        }
        else {
            fb = (K / B) * B;
        }
 
        // Multiple of C
        if ((K % C) != 0) {
 
            fc = ((K / C) + 1) * C;
        }
        else {
            fc = (K / C) * C;
        }
 
        // Store the resultant answer
        int ans;
        ans = Math.Min(fa - K, Math.Min(fc - K, fb - K));
        Console.WriteLine(ans);
    }
 
    // Drive code
    public static void Main(string[] args)
    {
        int A = 6, B = 10, C = 9, K = 2;
        solve(A, B, C, K);
    }
}
 
// This code is contributed by ukasp.

Javascript



输出
4

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