Codeforces 231A Team Solution & Explanation

Difficulty : 800

Problem Description

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.

Input

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.

Output

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

Examples

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

Note

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.

Understand this problem in 10 seconds:

This is a problem about a three-person team. They will be given n problems (input in the first line).

In each problem, each team member can provide a 0 or 1 as an answer, where 1 indicates that they are certain of the answer, and 0 means they are uncertain.

If a problem receives two or more certain answers (i.e., a count of 1s equal to or greater than two), they will record the answer. Return how much answers they will recode! (output)


Solution

C# Solution

Solution1

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);

We first read the total number of problems (n).

For each problem, we read a string of 0s and 1s representing the answers. We split this string by spaces to count the number of ‘1’s (indicating sure answers) in each problem. If a problem has 2 or more sure answers, we increment the result count.

Finally, we print the total count of problems with 2 or more sure answers.

Java Solution

Solution1

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 Solution

Solution1

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 Solution

Solution1

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);

Conclusion

Teamwork can be quite challenging to master. Sometimes, the team needs to figure out the most ‘problematic’ way to distribute all the problems, and trust me, that’s when the real fun begins haha~

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts or clicking ads, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *