📌  相关文章
📜  检查是否可以通过给定的合并对操作将 3 种不同颜色的数量转换为单一颜色

📅  最后修改于: 2021-10-27 06:18:57             🧑  作者: Mango

给定 3 个整数RGB,分别表示红色、绿色和蓝色的 3 种颜色的计数,这样相同数量的两种不同颜色(比如X )组合形成第三种颜色,其数量为2 * X的两倍。任务是检查是否可以将所有颜色转换为单一颜色。如果可能,则打印“是” 。否则,打印“否”

例子:

方法:将所有颜色更改为相同颜色意味着任务是达到T = (0, 0, R + G + B)或其其他两个排列中的任何一个的最终状态。最初,状态是 I = (R, G, B)。每次操作后,初始两种颜色的值各减一,第三种颜色增加二。根据所选颜色此操作可以写为(-1, -1, +2)的排列。因此,形成以下等式:

使用上面的等式,观察如果两个值在找到它们的模数为 3 后相等,则给定的颜色可以更改为一种单一颜色。因此,请按照以下步骤解决问题:

  1. 计算所有给定颜色的模 3。
  2. 检查相等的对。
  3. 如果发现是真的,打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check whether it is
// possible to do the operation or not
bool isPossible(int r, int b, int g)
{
 
    // Calculate modulo 3
    // of all the colors
    r = r % 3;
    b = b % 3;
    g = g % 3;
 
    // Check for any equal pair
    if (r == b || b == g || g == r) {
        return true;
    }
 
    // Otherwise
    else {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Given colors
    int R = 1, B = 3, G = 6;
 
    // Function Call
    if (isPossible(R, B, G)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to check whether
// it is possible to do the
// operation or not
static boolean isPossible(int r,
                          int b, int g)
{
    // Calculate modulo 3
    // of all the colors
    r = r % 3;
    b = b % 3;
    g = g % 3;
 
    // Check for any equal pair
    if (r == b || b == g || g == r)
    {
        return true;
    }
 
    // Otherwise
    else
    {
        return false;
    }
}
 
// Driver Code
public static void main(String[] args)
{
    // Given colors
    int R = 1, B = 3, G = 6;
 
    // Function Call
    if (isPossible(R, B, G))
    {
        System.out.print("Yes" + "\n");
    }
    else
    {
        System.out.print("No" + "\n");
    }
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to check whether it is
# possible to do the operation or not
def isPossible(r, b, g):
 
    # Calculate modulo 3
    # of all the colors
    r = r % 3
    b = b % 3
    g = g % 3
 
    # Check for any equal pair
    if(r == b or b == g or g == r):
        return True
 
    # Otherwise
    else:
        return False
 
# Driver Code
 
# Given colors
R = 1
B = 3
G = 6
 
# Function call
if(isPossible(R, B, G)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Shivam Singh


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to check whether
// it is possible to do the
// operation or not
static bool isPossible(int r,
                       int b, int g)
{
  // Calculate modulo 3
  // of all the colors
  r = r % 3;
  b = b % 3;
  g = g % 3;
 
  // Check for any equal pair
  if (r == b || b == g || g == r)
  {
    return true;
  }
 
  // Otherwise
  else
  {
    return false;
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given colors
  int R = 1, B = 3, G = 6;
 
  // Function Call
  if (isPossible(R, B, G))
  {
    Console.Write("Yes" + "\n");
  }
  else
  {
    Console.Write("No" + "\n");
  }
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
Yes

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