找到分数以二进制字符串形式给出的游戏的获胜者
给定一个表示排球比赛得分的二进制字符串。任务是根据以下条件找到比赛的获胜者:
- 在排球比赛中,两队互相比赛,先得到15分的球队获胜,除非两队都达到14分。
- 如果两队都达到 14 分,则保持领先 2 分的球队获胜。
在给定的二进制字符串中,0 表示 GEEK 的团队失去一分,1 表示 GEEK 的团队赢得一分。您必须确定 GEEK 的团队是赢了还是输了比赛。
例子:
Input : score[] = 01011111111110110101
Output : GEEK's won
Input : score[] = 010101010101010101010101010100
Output : GEEK's lost
案例一:当一队先得15分,而第二队得分不足15分时。然后遍历给定的二进制字符串并存储零和一的计数。在那之后,如果你得到一个计数为 15 并且计数为零小于 15,那么 GEEK 赢了,另一方面,如果你得到计数为零为 15 并且计数一个小于 15,那么 GEEK 输了。
情况二:当两队都得 14 分时,将双方的计数重置为零,并且对于每个零或一,他们的计数增加,同时检查 abs(count[0] – count[1]) 是否等于 2,如果这样如果发生这种情况,则意味着球队中的任何一支球队比其对手多得两分,并将成为获胜者。然后,您可以根据 count[0] 和 count[1] 的值找到获胜者。
下面是上述方法的实现:
C++
// Cpp program for predicting winner
#include
using namespace std;
// function for winner prediction
void predictWinner(string score, int n)
{
int count[2] = { 0 }, i;
for (i = 0; i < score.size(); i++) {
// increase count
count[score[i] - '0']++;
// check losing condition
if (count[0] == n && count[1] < n - 1) {
cout << "GEEKS lost";
return;
}
// check winning condition
if (count[1] == n && count[0] < n - 1) {
cout << "GEEKS won";
return;
}
// check tie on n-1 point
if (count[0] == n - 1 && count[1] ==
n - 1) {
count[0] = 0;
count[1] = 0;
break;
}
}
for (i++; i < score.size(); i++) {
// increase count
count[score[i] - '0']++;
// check for 2 point lead
if (abs(count[0] - count[1]) == 2) {
// condition of lost
if (count[0] > count[1])
cout << "GEEKS lost";
// condition of win
else
cout << "GEEKS won";
return;
}
}
}
// driver program
int main()
{
string score = "1001010101111011101111";
int n = 15;
predictWinner(score, n);
return 0;
}
Java
// Java program for
// predicting winner
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
// function for
// winner prediction
static void predictWinner(String score,
int n)
{
int count[] = new int[2], i;
for (i = 0;
i < score.length(); i++)
{
// increase count
count[score.charAt(i) - '0']++;
// check losing
// condition
if (count[0] == n &&
count[1] < n - 1)
{
System.out.print("GEEKS lost");
return;
}
// check winning condition
if (count[1] == n &&
count[0] < n - 1)
{
System.out.print("GEEKS won");
return;
}
// check tie on n-1 point
if (count[0] == n - 1 &&
count[1] == n - 1)
{
count[0] = 0;
count[1] = 0;
break;
}
}
for (i++; i < score.length(); i++)
{
// increase count
count[score.charAt(i) - '0']++;
// check for 2 point lead
if (Math.abs(count[0] -
count[1]) == 2)
{
// condition of lost
if (count[0] > count[1])
System.out.print("GEEKS lost");
// condition of win
else
System.out.print("GEEKS won");
return;
}
}
}
// Driver Code
public static void main(String[] args)
{
String score = "1001010101111011101111";
int n = 15;
predictWinner(score, n);
}
}
Python3
# Python 3 program for predicting winner
# function for winner prediction
def predictWinner(score, n):
count = [0 for i in range(2)]
for i in range(0, len(score), 1):
# increase count
index = ord(score[i]) - ord('0')
count[index] += 1
# check losing condition
if (count[0] == n and count[1] < n - 1):
print("GEEKS lost", end = " ")
return
# check winning condition
if (count[1] == n and count[0] < n - 1):
print("GEEKS won", end = " ")
return
# check tie on n-1 point
if (count[0] == n - 1 and
count[1] == n - 1):
count[0] = 0
count[1] = 0
break
i += 1
for i in range(i, len(score), 1):
# increase count
index = ord(score[i]) - ord('0')
count[index] += 1
# check for 2 point lead
if (abs(count[0] - count[1]) == 2):
# condition of lost
if (count[0] > count[1]):
print("GEEKS lost", end = " ")
# condition of win
else:
print("GEEKS won", end = " ");
return
# Driver Code
if __name__ == '__main__':
score = "1001010101111011101111"
n = 15
predictWinner(score, n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# program for predicting winner
using System;
class GFG
{
// function for winner prediction
public static void predictWinner(string score,
int n)
{
int[] count = new int[2]; int i;
for (i = 0; i < score.Length; i++)
{
// increase count
count[score[i] - '0']++;
// check losing
// condition
if (count[0] == n && count[1] < n - 1)
{
Console.Write("GEEKS lost");
return;
}
// check winning condition
if (count[1] == n && count[0] < n - 1)
{
Console.Write("GEEKS won");
return;
}
// check tie on n-1 point
if (count[0] == n - 1 &&
count[1] == n - 1)
{
count[0] = 0;
count[1] = 0;
break;
}
}
for (i++; i < score.Length; i++)
{
// increase count
count[score[i] - '0']++;
// check for 2 point lead
if (Math.Abs(count[0] - count[1]) == 2)
{
// condition of lost
if (count[0] > count[1])
{
Console.Write("GEEKS lost");
}
// condition of win
else
{
Console.Write("GEEKS won");
}
return;
}
}
}
// Driver Code
public static void Main(string[] args)
{
string score = "1001010101111011101111";
int n = 15;
predictWinner(score, n);
}
}
// This code is contributed by Shrikant13
Javascript
输出:
GEEKS won