📜  查找结果颜色组合

📅  最后修改于: 2021-06-01 00:34:44             🧑  作者: Mango

给定一个由三个颜色(G,B,Y)组成的字符串作为输入,任务是打印根据以下给出的规则形成的合成色:

// Rules for colour combination

Blue(B) * Green(G) = Yellow(Y)
Yellow(Y) * Blue(B) = Green(G)
Green(G) * Yellow(Y) = Blue(B)

例子:

Input: str = "GBYGB"
Output B

Input: str = "BYB"
Output Y

处理方法:该问题可以通过以下方式解决:

  1. 获取输入字符串。
  2. 将每个字母与其相邻字符进行比较。
  3. 使用以上条件来确定组合。
  4. 打印输出组合。

下面是上述方法的实现:

C++
// C++ program to find the
// resultant colour combination
 
#include 
using namespace std;
 
// Function to return Colour Combination
char Colour_Combination(string s)
{
    char temp = s[0];
 
    for (int i = 1; i < s.length(); i++) {
        if (temp != s[i]) {
 
            // Check for B * G = Y
            if ((temp == 'B' || temp == 'G')
                && (s[i] == 'G' || s[i] == 'B'))
                temp = 'Y';
 
            // Check for B * Y = G
            else if ((temp == 'B' || temp == 'Y')
                     && (s[i] == 'Y' || s[i] == 'B'))
                temp = 'G';
 
            // Check for Y * G = B
            else
                temp = 'B';
        }
    }
    return temp;
}
 
// Driver Code
int main(int argc, char** argv)
{
    string s = "GBYGB";
 
    cout << Colour_Combination(s);
}


Java
// Java program to find the
// resultant colour combination
class GfG
{
 
    // Function to return Colour Combination
    static char Colour_Combination(String s)
    {
        char temp = s.charAt(0);
     
        for (int i = 1; i < s.length(); i++)
        {
            if (temp != s.charAt(i))
            {
     
                // Check for B * G = Y
                if ((temp == 'B' || temp == 'G')
                            && (s.charAt(i) == 'G'
                            || s.charAt(i) == 'B'))
                    temp = 'Y';
     
                // Check for B * Y = G
                else if ((temp == 'B' || temp == 'Y')
                                && (s.charAt(i) == 'Y'
                                || s.charAt(i) == 'B'))
                    temp = 'G';
     
                // Check for Y * G = B
                else
                    temp = 'B';
            }
        }
        return temp;
    }
 
    // Driver code
    public static void main(String []args)
    {
        String s = "GBYGB";
        System.out.println(Colour_Combination(s));
    }
}
 
// This code is contributed by Rituraj Jain


Python3
# Python 3 program to find the
# resultant colour combination
 
# Function to return Colour Combination
def Colour_Combination(s):
    temp = s[0]
 
    for i in range(1, len(s), 1):
        if (temp != s[i]):
             
            # Check for B * G = Y
            if ((temp == 'B' or temp == 'G') and
                (s[i] == 'G' or s[i] == 'B')):
                temp = 'Y'
 
            # Check for B * Y = G
            elif ((temp == 'B' or temp == 'Y') and
                  (s[i] == 'Y' or s[i] == 'B')):
                temp = 'G'
 
            # Check for Y * G = B
            else:
                temp = 'B'
    return temp
 
# Driver Code
if __name__ == '__main__':
    s = "GBYGB"
 
    print(Colour_Combination(s))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find the
// resultant colour combination
     
using System;
 
class GFG
{
     
    // Function to return Colour Combination
    static char Colour_Combination(string s)
    {
        char temp = s[0];
     
        for (int i = 1; i < s.Length; i++)
        {
            if (temp != s[i])
            {
     
                // Check for B * G = Y
                if ((temp == 'B' || temp == 'G')
                    && (s[i] == 'G' || s[i] == 'B'))
                    temp = 'Y';
     
                // Check for B * Y = G
                else if ((temp == 'B' || temp == 'Y')
                        && (s[i] == 'Y' || s[i] == 'B'))
                    temp = 'G';
     
                // Check for Y * G = B
                else
                    temp = 'B';
            }
        }
        return temp;
    }
     
    // Driver Code
    static void Main()
    {
        string s = "GBYGB";
     
        Console.WriteLine(Colour_Combination(s));
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
B
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”