Codeforces 231A 團隊 – 翻譯與解答

難易度 : 800

英文原文如下

One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually offered several problems during programming contests. Long before the start the friends decided that they will implement a problem if at least two of them are sure about the solution. Otherwise, the friends won’t write the problem’s solution.

This contest offers n problems to the participants. For each problem we know, which friend is sure about the solution. Help the friends find the number of problems for which they will write a solution.

中文翻譯

有一天,三個最好的朋友 Petya, Vasya 跟 Tonya 決定組隊參加程式競賽。在程式競賽中,通常會提供多個問題給參賽者解決。很早以前,朋友們就決定,如果至少有兩個人對解決方案有信心,他們將執行一個問題的解決方案。否則,朋友們將不會撰寫該問題的解決方案。

這個比賽提供了 n 個問題給參賽者。對於每個問題,我們知道哪位朋友對解決方案有信心。幫助朋友們找出他們將會寫解決方案的問題數量。

輸入

The first input line contains a single integer n (1 ≤ n ≤ 1000) — the number of problems in the contest. Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the line equals 1, then Petya is sure about the problem’s solution, otherwise he isn’t sure. The second number shows Vasya’s view on the solution, the third number shows Tonya’s view. The numbers on the lines are separated by spaces.

第一行的輸入包含了一個整數 n (1 ≤ n ≤ 1000) – 也就是整個競賽的問題數。

接下來的 n 行各自包含了三個整數,每一個整數都是 0 或是 1。要是第一個數字等於1,則代表 Petya 對這個問題的解答有信心,反之則是不確定。第二個數字則是表示 Vasya 的,第三個數字是 Tonya 的。每行之中的數字們會用空格隔開。

輸出

Print a single integer — the number of problems the friends will implement on the contest.

印出一個整數 – 朋友們在比賽中會執行的問題數量。

範例

Input3
1 1 0
1 1 1
1 0 0
Output2
Input2
1 0 0
0 1 1
Output1

筆記

In the first sample Petya and Vasya are sure that they know how to solve the first problem and all three of them know how to solve the second problem. That means that they will write solutions for these problems. Only Petya is sure about the solution for the third problem, but that isn’t enough, so the friends won’t take it.

In the second sample the friends will only implement the second problem, as Vasya and Tonya are sure about the solution.

在第一個範例中, Petya 跟 Vasya 確定他們知道怎麼解決第一題,他們三個都知道可以解決第二個問題。那代表了他們會將那些題目寫下答案。另外,第三題只有 Petya 確定,所以他們不會採用它。

在第二個範例時,只有第二題有超過兩個人確定,分別是 Vasya 跟 Tonya。


解題思路

這一題我們可以使用一個簡單的雙重迴圈

以及一個不重複的計數器,來判斷一題中有多少位夥伴知道答案,要是大於等於2就會幫答題總數 +1

C#解決方案

方案1

int n = int.Parse(Console.ReadLine());  //total count of problems 

int res = 0;

for(int i = 0; i<n ; i++)
{
    int tmp = 0; //add to total if greater than 2
    string[] ans = Console.ReadLine().Split(' '); //split by space
    
    foreach(string a in ans)
    {
        tmp += a=="1"?1:0;
    }
    res += tmp>=2?1:0;
}

Console.WriteLine(res);

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine()); //total line
        int res = 0;

        for (int i = 0; i < n; i++) 
        {
            int tmp = 0; //tmp result, add to total if greater than or equal to 2
            String[] ans = scanner.nextLine().split(" ");

            for (String a : ans) 
            {
                tmp += a.equals("1") ? 1 : 0;
            }

            res += tmp >= 2 ? 1 : 0;
        }

        System.out.println(res);
    }
}

Python3解決方案

方案1

n = int(input())  #total line
res = 0

for i in range(n):
    tmp = 0  #tmp result, add to total if greater than or equal to 2
    ans = input().split(" ")

    for a in ans:
        tmp += 1 if a == "1" else 0

    res += 1 if tmp >= 2 else 0

print(res)

JavaScript解決方案

方案1

var n = readline();
var res = 0;

for (var i = 0; i < n; i++) {
    var tmp = 0;
    var ans = readline().split(' ');
    
    for (var j = 0; j < ans.length; j++) {
        tmp += ans[j] ==="1"?1:0;
    }
    res += tmp >= 2 ? 1 : 0;
}

print(res);

結論

這一題與團隊合作有關,讓人不禁想起了過去與夥伴一起參加程式競賽的時光呢~

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

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

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

題目連結 : Problem – A – Codeforces

一些其他的文章

發佈留言

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