c language test chip is good or bad


Problem description
has n (2 <n <20) chips, good or bad, and there are more than a known good chips and bad chips.
Each chip can be used to test other chips. When a good chip test with other chips, given that it can be correctly tested chip is good or bad. When a bad chip testing other chips, randomly give good or bad test results (that is, regardless of the actual results of the quality test of the chip).
The test results are given all the chips, and asked what the chip is a good chip.
The input format
of a data input operation is an integer n, which represents the number of chips.
The second line n + 1 is the behavior of n * n table, each row having n data. Table female makeup Data are each 0 or 1. The data in j-th column (1 <i, j <n ) represents the i-th row when the test results obtained with the test chips i j chips. 1 represents good, 0 bad, i = j is always 1 (the chip itself does not represent test results). Chip can not self-test.
Output formats
in order from smallest to largest number of all the good output chips.
Sample input
three
one hundred and one
010
one hundred and one
samples the output of
thirteen
C Language Reference answer:
include:
#define maximum 20

Main the In ()
{
int n-, text [20 is] [20 is], I, J, S; S // is the number of bad chips
scanf ( "% d", & n);

If the (n-> = 2 && n-<= 20 is)
{
for (I = 0; I <n-; I ++)
{
for (J = 0; J <n-; J ++)
{
Scanf ( "% D", & text [I] [J ]);
}
}
// remove the bad chip, to make a good chip
for (J = 0; J <n-; J ++)
{
S = 0;
for (I = 0; I <n-; I ++)
{
S + = text [I ] [J];
}
if (S> N / 2)
the printf ( "% D", J +. 1);

}
}
Return 0;
}

Excerpt:
The following two conclusions are useful. They are listed first.

If you randomly selected two-chip test, there will be
1) If the result is true, both are true or false.
2) For other results, at least one is wrong.

In any even chip, chip better than if the bad chip, all chips are divided into two groups. According to the principle of the drawer, there is
1) a group must have two good chip.
2) the number of good chips must be greater than the number of bad chips.

Test procedure
1) The chip is divided into groups 1 and 2, 3 and 4. 2K-1 and 2K. If we test each other, we have to get the same results with the real group.
2) retain the same set of real results and discard other groups. Certainly more good than bad chipset chipset. (Therefore, only when two or more groups is true, they must be true, the end of the test)
3) If the result is true, the chip will be good or bad, so that half can be discarded. All the same from a real group, any remove a discard another to form a new test group, two groups of two or continued until the same group only two real or a test, stick to the end is a good chip.

Explanation: The same group may become real strange. When the array is odd, select any group (assumed as A) A test group and the remaining one, if the test result A is greater than or equal to half of the chip, the A better end of the test. Otherwise, A is a bad chip. If A is a good chip, it must be a bad chip. After removing the remaining part, forming a new test group and two or two groups continued.

The general principle is similar to the gold rush. At first there were a lot of good chips. When we remove the chip, we must ensure that the number of bad chips must be greater than or equal to the number of good chips. In this way, we can ensure that the remaining number of chips in good chip is more than a bad chip. When an odd number of groups, more than half of the valid votes (as effective as greater than good or bad, it is good to be equal).

Since at least one half of the chip is removed, the worst case is a time to remove half of the chip, and reduced in equal proportions. When there are n chips, number of tests n + (n / 2) + (n / 4) ... = 2n.
Special amnesty

Guess you like

Origin www.cnblogs.com/blogst/p/10942521.html