三个玩家 P1、P2 和 P3 正在玩游戏。但是一次只能有两个玩家玩游戏,所以他们决定,一次两个玩家玩游戏,一个观看。当一场比赛结束时,输掉比赛的人成为下一场比赛的观众,而观看比赛的人与获胜者进行比赛。任何游戏都不能有平局。玩家 P1 和 P2 将进行第一场比赛。输入是玩了 n 场比赛的次数,并且在下一场比赛中获胜的 n 场比赛。
例子 :
Input :
No. of Games : 4
Winner of the Game Gi : 1 1 2 3
Output : YES
Explanation :
Game1 : P1 vs P2 : P1 wins
Game2 : P1 vs P3 : P1 wins
Game3 : P1 vs P2 : P2 wins
Game4 : P3 vs P2 : P3 wins
None of the winners were invalid
Input :
No. of Games : 2
Winner of the Game Gi : 2 1
Output : NO
Explanation :
Game1 : P1 vs P2 : P2 wins
Game2 : P2 vs P3 : P1 wins (Invalid winner)
In Game2 P1 is spectator
方法 :
从 P1 和 P2 开始,继续游戏,同时通过从三名玩家的总和(即 6)中减去当前的观众和获胜者,使输家成为新的观众。无效的输入将停止该过程。
下面是上述方法的实现:
C++
// C++ program to check if the game
// is valid
#include
using namespace std;
string check_valid(int a[], int n)
{
// starting with player P1 and P2
// so making P3 spectator
int spec = 3;
for (int i = 0; i < n; i++) {
// If spectator wins a game
// then its not valid
if (a[i] == spec) {
return "Invalid";
}
// subtracting the current spectator
// and winner from total sum 6 which
// makes losing player spectator
spec = 6 - a[i] - spec;
}
// None of the winner is found spectator.
return "Valid";
}
// Driver program to test above functions
int main()
{
int n = 4;
int a[n] = {1, 1, 2, 3};
cout << check_valid(a, n);
return 0;
}
Java
// Java program to check if the game
// is valid
import java.io.*;
class GFG {
static String check_valid(int a[], int n)
{
// starting with player P1 and P2
// so making P3 spectator
int spec = 3;
for (int i = 0; i < n; i++) {
// If spectator wins a game
// then its not valid
if (a[i] == spec) {
return "Invalid";
}
// subtracting the current spectator
// and winner from total sum 6 which
// makes losing player spectator
spec = 6 - a[i] - spec;
}
// None of the winner is found spectator.
return "Valid";
}
// Driver program to test above functions
public static void main (String[] args) {
int a[] = {1, 1, 2, 3};
int n = a.length;
System.out.println(check_valid(a, n));
}
}
// This code is contributed by vt_m
Python3
# Python3 code to check if the game
# is valid
def check_valid( a , n ):
# starting with player P1 and P2
# so making P3 spectator
spec = 3
for i in range(n):
# If spectator wins a game
# then its not valid
if a[i] == spec:
return "Invalid"
# subtracting the current spectator
# and winner from total sum 6 which
# makes losing player spectator
spec = 6 - a[i] - spec
# None of the winner is found spectator.
return "Valid"
# Driver code to test above functions
n = 4
a = [1, 1, 2, 3]
print(check_valid(a, n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to check if the game
// is valid
using System;
class GFG {
static String check_valid(int []a, int n)
{
// starting with player P1 and P2
// so making P3 spectator
int spec = 3;
for (int i = 0; i < n; i++) {
// If spectator wins a game
// then its not valid
if (a[i] == spec) {
return "Invalid";
}
// subtracting the current spectator
// and winner from total sum 6 which
// makes losing player spectator
spec = 6 - a[i] - spec;
}
// None of the winner is found spectator.
return "Valid";
}
// Driver program
public static void Main ()
{
int []a = {1, 1, 2, 3};
int n = a.Length;
Console.WriteLine(check_valid(a, n));
}
}
// This code is contributed by vt_m
PHP
Javascript
输出 :
Valid
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。