📌  相关文章
📜  通过将A,B和C相加总值N来使它们相等

📅  最后修改于: 2021-04-26 05:46:58             🧑  作者: Mango

给定3个整数A,B和C ,以及整数N ,任务是在所有其他3个数字之间分配N ,以使末尾的A = B = C。如果可以分发,则打印“是”,否则输出“否”。
例子:

方法:
为了解决上述问题,我们必须遵循以下步骤:

  • 在所有三个整数A,B和C中找出最大值。将其设为整数K
  • 将整数K乘以3,然后再将其乘以三个整数的和。
  • 检查该数字与N的差是否可被3整除
  • 如果是,则输出为“是”,否则无法分配该号码。

下面是上述方法的实现:

C++
// C++ program to distribute integer N 
// among A, B, C such that they become equal 
#include
using namespace std;
  
void distributeN(int A, int B, int C, int n)
{
    // Find maximum among the three elements 
    int max1 = max(A, B);
    int max2 = max(B, C);
    int maximum = max(max1, max2);
      
    // Summation of three elements 
    int sum = A + B + C;
    int p = (3 * maximum) - sum;
    int diff = n - p;
      
    // Check if difference is divisible by 3 
    if (diff < 0 || diff % 3) 
        cout << "No";
    else
        cout << "Yes";
}
          
// Driver code
int main()
{
    int A = 10, B = 20;
    int C = 15, n = 14;
      
    distributeN(A, B, C, n);
    return 0;
}
  
// This code is contributed by PratikBasu


Java
// Java program to distribute integer N 
// among A, B, C such that they become equal 
class GFG{
  
static void distributeN(int A, int B, int C, 
                        int n)
{
      
    // Find maximum among the three elements 
    int max1 = Math.max(A, B);
    int max2 = Math.max(B, C);
    int maximum = Math.max(max1, max2);
      
    // Summation of three elements 
    int sum = A + B + C;
    int p = (3 * maximum) - sum;
    int diff = n - p;
      
    // Check if difference is divisible by 3 
    if (diff < 0 || diff % 3 == 0) 
        System.out.print("No");
    else
        System.out.print("Yes");
}
          
// Driver code
public static void main(String[] args)
{
    int A = 10, B = 20;
    int C = 15, n = 14;
      
    distributeN(A, B, C, n);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 Program to Distribute integer N 
# among A, B, C such that they become equal
  
def distributeN(A, B, C, n):
      
    # find maximum among the three elements
    maximum = max(A, B, C)
      
    # summation of three elements
    sum = A + B+C
      
      
    p = (3 * maximum)-sum
      
    diff = n-p
      
    # check if difference is divisible by 3
    if diff < 0 or diff % 3: 
        print "No" 
    else:
        print "Yes"
          
          
# Driver code 
A = 10
B = 20
C = 15 
n = 14 
distributeN(A, B, C, n)


C#
// C# program to distribute integer N 
// among A, B, C such that they become equal 
using System;
  
class GFG{
  
static void distributeN(int A, int B, int C, 
                        int n)
{
      
    // Find maximum among the three elements 
    int max1 = Math.Max(A, B);
    int max2 = Math.Max(B, C);
    int maximum = Math.Max(max1, max2);
      
    // Summation of three elements 
    int sum = A + B + C;
    int p = (3 * maximum) - sum;
    int diff = n - p;
      
    // Check if difference is divisible by 3 
    if (diff < 0 || diff % 3 == 0) 
        Console.Write("No");
    else
        Console.Write("Yes");
}
          
// Driver code
public static void Main(String[] args)
{
    int A = 10, B = 20;
    int C = 15, n = 14;
      
    distributeN(A, B, C, n);
}
}
  
// This code is contributed by Rajput-Ji


输出:
No

时间复杂度: O(1)