给定3个整数R , G和B,分别表示红色,绿色和蓝色3种颜色的数量,以使相同数量(例如X )的两种不同颜色结合起来,形成第二种颜色2 * X的第三种颜色。任务是检查是否有可能将所有颜色转换为单一颜色。如果可能,请打印“是” 。否则,打印“否” 。
例子:
Input: R = 1, G = 1, B = 1
Output: Yes
Explanation:
Operation 1: Mix 1 unit of Red with 1 unit of Blue to obtain 2 units of Green.
Therefore, count of each colors are: R = 0, G = 3, B = 0
Hence, all the colors are converted to a single color.
Input: R = 1, G = 6, B = 3
Output: Yes
Explanation:
Operation 1: Mix 1 unit of Red with 1 unit of Green to obtain 2 units of Blue.
Therefore, count of each colors are: R = 0, G = 5, B = 5
Operation 2: Mix 5 units of Green with 5 units of Blue to obtain 10 units of Red.
Therefore, count of each colors are: R = 10, G = 0, B = 0
Hence, all the colors are converted to a single color.
方法:将所有颜色更改为相同的颜色意味着任务是达到最终状态,即T =(0,0,R + G + B)或其他两个排列中的任意一个。最初,状态为I =(R,G,B)。每次操作后,最初的两种颜色的值分别减小一个,而对于第三种颜色的值则增大两个。可以将这种操作写为基于所选颜色的(-1,-1,+ 2)排列。因此,形成以下等式:
N * op ≡ T mod 3 where,
N is the number of operations to be performed
op is the operation
T is the final state
使用上面的方程式,观察到两个值的模数等于3后,如果两个值相等,则可以将给定的颜色更改为一种颜色。因此,请按照以下步骤解决问题:
- 计算所有给定颜色的模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)