📜  检查游戏是否有效

📅  最后修改于: 2021-04-23 21:49:19             🧑  作者: Mango

三个玩家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