Codeforces 96A Football 足球 翻譯與解答

難易度 : 900

英文原文如下

Petya loves football very much. One day, as he was watching a football match, he was writing the players’ current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. 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 considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not.

中文翻譯

Petya 非常喜歡足球。有一天,他在觀看一場足球比賽,然後他將場上球員目前的位置寫在紙上。為了簡化整個情況,他將其寫成了一個包含 0 跟 1 的字串。 一個 0 代表了其中一隊的一個球員,一個 1 則代表另一隊的一個球員。如果有至少7個同隊的人站在一起,那代表著整個情況非常危險。舉例來說, 00100110111111101是危險的,11110111011101 則不是。給予你現在的情況,判斷現在是否為危險。

輸入

The first input line contains a non-empty string consisting of characters “0” and “1”, which represents players. The length of the string does not exceed 100 characters. There’s at least one player from each team present on the field.

第一行輸入包含了一個非空白的字串,由 “0” 跟 “1” 組成,代表著玩家。字串的長度不會超過100個字元,而且各隊至少各會有一個玩家。

輸出

Print “YES” if the situation is dangerous. Otherwise, print “NO”.

要是整個情況是危險的,印出 “YES”,不然就印出 “NO”。

範例

輸入001001
輸出NO
輸入1000000001
輸出YES


解題思路

C#解決方案

方案1

string word = Console.ReadLine();
char now = word[0];
int cnt = 1;
bool dangerous = false;

for (int i = 1; i < word.Length; i++)
{
    char c = word[i];
    if(c == now){
        cnt+=1;
        if(cnt==7){
            Console.WriteLine("YES");
            dangerous = true;
            break;
        }
    }
    else{
        now = c;
        cnt=1;
    }
}

if(!dangerous){
    Console.WriteLine("NO");
}

用一個簡單的迴圈以及幾個變數就能輕鬆解決

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.nextLine();
        char now = word.charAt(0);
        int cnt = 1;
        boolean dangerous = false; 
        
        
        for (char c : word.toCharArray()) {
           if(c == now){
                cnt+=1;
                if(cnt==7){
                    System.out.print("YES");
                    dangerous = true;
                    break;
                }
            }
            else{
                now = c;
                cnt=1;
            } 
        }
        
        if(!dangerous){
            System.out.print("NO");
        }
    }
}

結論

🧡如果文章有幫上你的忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 96A – Codeforces

一些其他的Codeforces文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *