Exam questions basic exercise chip test (Java)

Exam questions basic exercise chip test

Resource limit

Time limit: 1.0s Memory limit: 512.0MB

Problem Description

  There are n (2≤n≤20) chips, good or bad. It is known that there are more good chips than bad ones.  Each chip can be used to test other chips. When testing other chips with a good chip, it can correctly give out whether the tested chip is good or bad. When using a bad chip to test other chips, it will randomly give good or bad test results (that is, this result has nothing to do with the actual quality of the tested chip).  Give the test results of all chips and ask which chips are good chips.

Input format

  The first line of input data is an integer n, indicating the number of chips.  The second row to the n + 1th row are n * n tables with n data in each row. Each data in the table is 0 or 1. The data in the i-th row and j-th column (1≤i, j≤n) in the n-rows represents the test results obtained when testing the j-th chip with the i-th chip , 1 means good, 0 means bad, when i = j, it will always be 1 (does not mean the test result of the chip on itself. The chip cannot test itself).

Output format

  Output the numbers of all good chips in order from small to large

Sample input

3 1 0 1 0 1 0 1 0 1

Sample output

1 3

Code

 1 import java.util.Scanner;
 2 import java.util.*;
 3  4  5 public class Main {
 6     static int n,a[][],count[];
 7     
 8     public static void main(String[] args) {
 9         Scanner sc = new Scanner(System.in);
10         n = sc.nextInt();
11         a = new int[22][22];
12         count = new int [25];
13         for(int i=1;i<=n;i++){
14             for(int j=1;j<=n;j++){
15                 a[i][j] = sc.nextInt();
16             }
17         }
18             
19         for(int i=1;i<=n;i++){
20             for(int j=1;j<=n;j++){
21                 if(i!=j&&a[i][j]==1)
22                     count[j]++;
23             }
24         }
25         for(int j=1;j<=n;j++)
26             if(count[j]>=n/2)
27                 System.out.print(j + " ");
28     }
29 30 }

 

to sum up

It was confusing at first, I didn't know what to do, and then I wrote a simulation for a long time, but in the end it didn't. Then I went to read the blogs of other bigwigs and found out that the drawer principle was used, but I didn't quite understand why it can be used here.

In other words, as long as the number of 1> the number of 0, we can think that this chip is good. so amazing! Then the code is like the above.



Guess you like

Origin www.cnblogs.com/acm-cyz/p/12740326.html