检查游戏是否有效

三名玩家P1、P2和P3正在玩一个游戏。但一次只有两名玩家可以玩游戏,所以他们决定,一次两名玩家将玩游戏,一名玩家将观看。当一场比赛结束时,输掉比赛的人成为下一场比赛的观众,观看比赛的人与赢家比赛。任何一场比赛都不可能有平局。玩家P1和P2将玩第一个游戏。输入是n个游戏的游戏数,下一行是n个游戏的赢家。

null

例如:

Input : No. of Games : 4Winner of the Game Gi : 1 1 2 3 Output : YESExplanation : Game1 : P1 vs P2 : P1 winsGame2 : P1 vs P3 : P1 winsGame3 : P1 vs P2 : P2 winsGame4 : P3 vs P2 : P3 winsNone of the winners were invalidInput : No. of Games : 2Winner of the Game Gi : 2 1 Output : NOExplanation : Game1 : P1 vs P2 : P2 winsGame2 : 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 <bits/stdc++.h>
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

<?php
// PHP program to check if
// the game is valid
function check_valid( $a , $n )
{
// starting with player P1 and P2
// so making P3 spectator
$spec = 3;
for ( $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 Code
$n = 4;
$a = array (1, 1, 2, 3);
echo check_valid( $a , $n );
// This code is contributed by vt_m
?>


Javascript

<script>
// Javascript program to check if the game
// is valid
function check_valid(a, n)
{
// Starting with player P1 and P2
// so making P3 spectator
let spec = 3;
for (let 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 code
let a = [ 1, 1, 2, 3 ];
let n = a.length;
document.write(check_valid(a, n));
// This code is contributed by sanjoy_62
</script>


输出:

Valid

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享