📜  洗牌后找出球的正确位置

📅  最后修改于: 2021-09-24 03:20:15             🧑  作者: Mango

考虑一个随机游戏。有3 个玻璃杯,编号从1 到 3 ,其中任何一个玻璃杯下都藏有一个球。然后任意 2 个眼镜被洗牌。该操作进行3次。
给定一个范围为[1, 3]的整数N和 3相同范围的整数。第N 个玻璃最初包含球,每对给定的整数代表需要洗牌的玻璃指数。请记住,每次洗牌后,眼镜都会重新编号。
任务是在所有shuffle操作后找出包含球的玻璃的索引。
例子:

方法:最简单的方法是为每个 shuffle 操作运行一个循环。
如果被洗牌的 2 个玻璃杯中的任何一个包含球,那么很明显将N的值更改为被洗牌的玻璃杯的索引。
如果 2 个洗牌杯中的任何一个不包含球,则无需执行任何操作。
下面是上面代码的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
const int M = 3, N = 2;
 
// Function to generate the index of the glass
// containing the ball
void getIndex(int n, int shuffle[][N])
{
    for (int i = 0; i < 3; i++) {
 
        // Checking if the glasses
        // being shuffled contain
        // the ball
 
        // Change the index
        if (shuffle[i][0] == n)
            n = shuffle[i][1];
 
        // Change the index
        else if (shuffle[i][1] == n)
            n = shuffle[i][0];
    }
 
    // Print the index
    cout << n;
}
 
// Driver's Code
int main()
{
    int n = 3;
 
    // Storing all the shuffle operation
    int shuffle[M][N] = {
        { 3, 1 },
        { 2, 1 },
        { 1, 2 }
    };
 
    getIndex(n, shuffle);
}


Java
// Java implementation of the above approach
import java.io.*;
 
class GFG
{
static int M = 3;
static int N = 2;
 
// Function to generate the index of the glass
// containing the ball
static void getIndex(int n, int shuffle[][])
{
    for (int i = 0; i < 3; i++)
    {
 
        // Checking if the glasses
        // being shuffled contain
        // the ball
 
        // Change the index
        if (shuffle[i][0] == n)
            n = shuffle[i][1];
 
        // Change the index
        else if (shuffle[i][1] == n)
            n = shuffle[i][0];
    }
 
    // Print the index
    System.out.println (n);
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 3;
     
    // Storing all the shuffle operation
    int shuffle[][] = {{ 3, 1 },
                       { 2, 1 },
                       { 1, 2 }};
     
    getIndex(n, shuffle);
}
}
 
// This code is contributed by ajit.


Python3
# Python3 implementation of
# the above approach
M = 3; N = 2;
 
# Function to generate the index of
# the glass containing the ball
def getIndex(n, shuffle) :
 
    for i in range(3) :
 
        # Checking if the glasses
        # being shuffled contain
        # the ball
 
        # Change the index
        if (shuffle[i][0] == n) :
            n = shuffle[i][1];
 
        # Change the index
        elif (shuffle[i][1] == n) :
            n = shuffle[i][0];
 
    # Print the index
    print(n);
 
# Driver Code
if __name__ == "__main__" :
 
    n = 3;
 
    # Storing all the shuffle operation
    shuffle = [[ 3, 1 ],
               [ 2, 1 ],
               [ 1, 2 ]];
 
    getIndex(n, shuffle);
 
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach
using System;
     
class GFG
{
static int M = 3;
static int N = 2;
 
// Function to generate the index of
// the glass containing the ball
static void getIndex(int n, int [,]shuffle)
{
    for (int i = 0; i < 3; i++)
    {
 
        // Checking if the glasses
        // being shuffled contain
        // the ball
 
        // Change the index
        if (shuffle[i, 0] == n)
            n = shuffle[i, 1];
 
        // Change the index
        else if (shuffle[i, 1] == n)
            n = shuffle[i, 0];
    }
 
    // Print the index
    Console.WriteLine(n);
}
 
// Driver Code
public static void Main (String[] args)
{
    int n = 3;
     
    // Storing all the shuffle operation
    int [,]shuffle = {{ 3, 1 },
                      { 2, 1 },
                       { 1, 2 }};
     
    getIndex(n, shuffle);
}
}
     
// This code is contributed by Princi Singh


Javascript


输出:
1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程