考虑一个随机游戏。有3 个玻璃杯,编号从1 到 3 ,其中任何一个玻璃杯下都藏有一个球。然后任意 2 个眼镜被洗牌。该操作进行3次。
给定一个范围为[1, 3]的整数N和 3对相同范围的整数。第N 个玻璃最初包含球,每对给定的整数代表需要洗牌的玻璃指数。请记住,每次洗牌后,眼镜都会重新编号。
任务是在所有shuffle操作后找出包含球的玻璃的索引。
例子:
Input:
N = 3
3 1
2 1
1 2
Output: 1
Firstly the 3rd glass contain the ball.
After the first shuffle operation (3, 1), 1st glass contain the ball.
After the second shuffle operation (2, 1), 2nd glass contain the ball.
After the third shuffle operation (1, 2), 1st glass contain the ball.
Input:
N = 1
1 3
1 2
2 3
Output: 2
方法:最简单的方法是为每个 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 现场工作专业课程和学生竞争性编程现场课程。