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

📅  最后修改于: 2021-06-25 13:13:38             🧑  作者: 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)