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

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

考虑随机播放游戏。有3个编号从1到3的玻璃杯,其中一个球藏在任何一个玻璃杯的下面。然后,将任意两副眼镜洗净。进行此操作3次。

给定一个整数N,范围为[1、3]和3相同范围的整数。第N个玻璃杯最初包含球,每对给定的整数表示需要重新排列的玻璃杯的索引。请记住,每次洗牌后,眼镜都会重新编号。

任务是在所有洗牌操作之后找出包含球的玻璃的索引。

例子:

方法:最简单的方法是为每个随机操作运行一个循环。

如果要洗牌的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


输出:
1