Blue Chip Bridge Cup practice test questions basic Python and C ++

Chip test questions basic training

By submitting this question  

Resource constraints

Time limit: 1.0s Memory Limit: 512.0MB

Problem Description

  There are n (2≤n≤20) chips, good and bad, worse than the known good die chip.
  Each chip can be used to test other chips. Hershey with other chip test chip, can be given the right test chip is good or bad. But with bad chip testing other chips, random give good or bad test results (i.e., the result is independent of the actual test chip is good or bad).
  All test results are given chip, chip ask what is a good chip.

Input Format

  A first line data input integer n, the number of chips.
  The second row to n + 1 n * n behavior of a table, each row of n data. Each data table is 0 or 1, in this i-th row in the n-th row j-th column (1≤i, j≤n) data representing test results obtained by the i-th block of the j-chip test chips 1 represents good, bad 0 indicates, when I 1 = j is uniformly (the test result does not indicate that the chip itself. chip itself can not be tested).

Output Format

  In ascending order of the number of outputs of all the good chips

Sample input

3
1 0 1
0 1 0
1 0 1

Sample Output

1 3

Analysis: The title is more than half true, natural, detected more than half is true. That is, the column 1 is greater than n / 2.

Python version

n = int(input())
l, ans = [], []
for _ in range(n):
    l.append(list(map(int, input().split())))
for i in range(n):
    ans.append(i + 1) if [x[i] for x in l].count(1) > n // 2 else ans
print(*ans)

 

C ++ version

#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    int a[20][20];
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            cin >> a[i][j];
        }
    }
    for(int i = 0; i < n; i++) {
        int cnt = 0;
        for(int j = 0; j < n; j++) {
            if(j != i)
                cnt += a[j][i];
        }
        if(cnt >= n/2) {
            cout << i+1 << " ";
        }
    }
    return 0;
}

 

Published 75 original articles · won praise 139 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43906799/article/details/105015862