首页 > 文章列表 > C++程序用于检查足球比赛中球员位置是否危险

C++程序用于检查足球比赛中球员位置是否危险

C程序 足球比赛 球员位置
361 2023-08-31

Suppose we have a binary string S of size n. Amal loves football very much. One day, he was watching a match, he was writing the players' current positions on a piece of paper. The given string is the written position. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is marked as "dangerous". We have to check whether it is dangerous or not.

Problem Category

To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they 通常是各种应用程序中首选的数据类型,并且被用作输入的数据类型 and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.

https://www.tutorialspoint.com/cplusplus/cpp_strings.htm

https://www.tutorialspoint.com/cprogramming/c_strings.htm

So, if the input of our problem is like S = "1000000001001", then the output will be True, as 有8个>7个连续的0。

步骤

为了解决这个问题,我们将按照以下步骤进行:

if "0000000" is in S, then:
   return true
otherwise when "1111111" is in S, then:
   return true
return false

示例

让我们看下面的实现以获得更好的理解 −

#include <bits/stdc++.h>
using namespace std;
bool solve(string S){
   if (S.find("0000000") != string::npos){
      return true;
   }
   else if (S.find("1111111") != string::npos){
      return true;
   }
   return false;
}
int main(){
   string S = "1000000001001";
   cout << solve(S) << endl;
}

Input

"1000000001001"

输出

1