C + + implementation: chip testing

Keywords: C++

Problem description
There are n (2 ≤ n ≤ 20) chips, good and bad. It is known that there are more good chips than bad ones.
Each chip can be used to test other chips. When other chips are tested with a good chip, it can correctly give whether the chip under test is good or bad. When testing other chips with bad chips, good or bad test results will be given randomly (that is, this result has nothing to do with the actual quality of the chip being tested).
Give the test results of all chips and ask which chips are good ones.
Input format
The first line of input data is an integer n, indicating the number of chips.
The second row to the n+1 row is a table of n*n, with N data in each row. Each data i n the table is 0 or 1. The data in row I and column J (1 ≤ i, j ≤ n) in row n represents the test results obtained when the j-th chip is tested with the i-th chip. 1 represents good, 0 represents bad, and i=j is all 1 (does not mean the test results of the chip itself). The chip cannot test itself).
Output format
Output the numbers of all good chips in the order of small to large
sample input
3
1 0 1
0 1 0
1 0 1
sample output
1 3
 
Idea: it is known that the number of good chips is more than that of bad chips. When a good chip is encountered, it is possible to use the bad chip to test. It is possible that the test result is correct and may be wrong. If the qualified chip is used to test, the test result must be qualified. So if the number of other chips is greater than half of the total number of chips when a chip is tested, it is OK It shows that the chip being tested is a good chip.
 
 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 class chips
 6 {
 7 public:
 8     int get_n()  //Get the number of chips
 9     {
10         cin >> n;
11         return n;
12     }
13 
14     int get_all_chips()   //Initialize all chips
15     {
16         for(int i=1;i<=n;i++)
17         {
18             for(int j=1;j<=n;j++)
19             {
20                 cin>>all_chips[i][j];
21             }
22         }
23         return 0;
24     }
25 
26     int check_chips()  //Select qualified chips
27     {
28         for(int i=1;i<=n;i++)
29         {
30             for(int j=1;j<=n;j++)
31             {
32                 if(all_chips[i][j]==1)
33                 {
34                     qualified_chips[j]++;
35                 }
36             }
37         }
38     }
39 
40     int print_qualified_chips()  //Output function
41     {
42         int flag=0;   //use flag To control the output format
43         for(int i=1;i<=n;i++)
44         {
45             if(qualified_chips[i]>n/2)
46             {
47                 if(flag==0)
48                 {
49                     cout << i;
50                     flag=1;
51                 }
52                 else
53                 {
54                     cout<<" "<<i;
55                 }
56             }
57         }
58     }
59 private:
60     int all_chips[21][21];   //All chips
61     int qualified_chips[21]={0};   //Qualified chip
62     int n;
63 };
64 
65 int main(void)
66 {
67     chips chip;
68     chip.get_n();
69     chip.get_all_chips();
70     chip.check_chips();
71     chip.print_qualified_chips();
72     return 0;
73 }

Note: i=j does not count, so when judging whether the chip meets the conditions, use > instead of > =. If you take it into consideration, add an equal sign.

Posted by TheFilmGod on Fri, 03 Apr 2020 14:22:13 -0700