📌  相关文章
📜  使三个数字等于给定的运算

📅  最后修改于: 2021-05-06 21:05:28             🧑  作者: Mango

给定四个正整数ABCK。任务是检查是否有可能借助K使三个整数ABC相等,并使K等于0 。在一个操作中,您可以从K中减去任何值(如果在减去后仍保持大于0的值),并将新值添加到三个整数ABC中的任何一个

例子:

的方法:检查是否有可能通过排序三个数字,并通过3和第2元件和31元素的差的总和中减去K的值以均衡三个数字。如果K仍大于0且可以在三个元素之间平均分配,则只有三个元素可以相等,而K可以等于0

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if a, b and c can
// be made equal with the given operations
bool canBeEqual(int a, int b, int c, int k)
{
    int arr[3];
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
  
    // Sort the three numbers
    sort(arr, arr + 3);
  
    // Find the sum of difference of the 3rd and
    // 2nd element and the 3rd and 1st element
    int diff = 2 * arr[2] - arr[1] - arr[0];
  
    // Subtract the difference from k
    k = k - diff;
  
    // Check the required condition
    if (k < 0 || k % 3 != 0)
        return false;
  
    return true;
}
  
// Driver code
int main()
{
    int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
  
    if (canBeEqual(a1, b1, c1, k1))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function that returns true if a, b and c can
// be made equal with the given operations
static boolean canBeEqual(int a, int b, int c, int k)
{
    int []arr = new int[3];
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
  
    // Sort the three numbers
    Arrays.sort(arr);
  
    // Find the sum of difference of the 3rd and
    // 2nd element and the 3rd and 1st element
    int diff = 2 * arr[2] - arr[1] - arr[0];
  
    // Subtract the difference from k
    k = k - diff;
  
    // Check the required condition
    if (k < 0 || k % 3 != 0)
        return false;
  
    return true;
}
  
// Driver code
public static void main(String[] args)
{
    int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
  
    if (canBeEqual(a1, b1, c1, k1))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach 
  
# Function that returns true if a, b and c can 
# be made equal with the given operations 
def canBeEqual(a, b, c, k) : 
  
    arr = [0] * 3; 
    arr[0] = a; 
    arr[1] = b; 
    arr[2] = c; 
  
    # Sort the three numbers 
    arr.sort()
  
    # Find the sum of difference of the 3rd and 
    # 2nd element and the 3rd and 1st element 
    diff = 2 * arr[2] - arr[1] - arr[0]; 
  
    # Subtract the difference from k 
    k = k - diff; 
  
    # Check the required condition 
    if (k < 0 or k % 3 != 0) :
        return False; 
  
    return True; 
  
# Driver code 
if __name__ == "__main__" : 
  
    a1 = 6; b1 = 3; c1 = 2; k1 = 7; 
  
    if (canBeEqual(a1, b1, c1, k1)) :
        print("Yes"); 
    else :
        print("No"); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
// Function that returns true if a, b and c can
// be made equal with the given operations
static bool canBeEqual(int a, int b, int c, int k)
{
    int []arr = new int[3];
    arr[0] = a;
    arr[1] = b;
    arr[2] = c;
  
    // Sort the three numbers
    Array.Sort(arr);
  
    // Find the sum of difference of the 3rd and
    // 2nd element and the 3rd and 1st element
    int diff = 2 * arr[2] - arr[1] - arr[0];
  
    // Subtract the difference from k
    k = k - diff;
  
    // Check the required condition
    if (k < 0 || k % 3 != 0)
        return false;
  
    return true;
}
  
// Driver code
public static void Main(String[] args)
{
    int a1 = 6, b1 = 3, c1 = 2, k1 = 7;
  
    if (canBeEqual(a1, b1, c1, k1))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by 29AjayKumar


输出:
Yes