Encuentre la combinación de colores resultante

Dada una string de tres colores (G, B, Y) como entrada, la tarea es imprimir el color combinado resultante formado de acuerdo con la regla que se indica a continuación: 

// Rules for colour combination

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

Ejemplos: 

Input: str = "GBYGB"
Output B

Input: str = "BYB"
Output Y

Enfoque: Este problema se puede resolver de la siguiente manera: 

  1. Obtenga la string de entrada.
  2. Compara cada alfabeto con sus caracteres adyacentes.
  3. Utilice la condición anterior para determinar la combinación.
  4. imprimir la combinación de salida.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ program to find the
// resultant colour combination
 
#include <iostream>
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

<?php
// PHP program to find the
// resultant colour combination
 
// Function to return Colour Combination
function Colour_Combination($s)
{
    $temp = $s[0];
 
    for ($i = 1; $i < strlen($s); $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
$s = "GBYGB";
 
echo Colour_Combination($s);
 
// This code is contributed by ita_c
?>

Javascript

<script>
 
// Javascript program to find the
// resultant colour combination
 
// Function to return Colour Combination
function Colour_Combination(s)
{
    let temp = s[0];
 
    for(let 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
let s = "GBYGB";
 
document.write(Colour_Combination(s));
 
// This code is contributed by Surbhi Tyagi.
 
</script>
Producción: 

B

 

Publicación traducida automáticamente

Artículo escrito por VISHAL_PERIYASAMY_R y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *