Pearl (bead), pearl bead
Description
There are n pearls of the same shape and size, and their weights are different. N is an integer. All pearls are numbered from 1 to n. Your task is to find the weight of a pearl in the middle, that is, the weight column (n + 1)/2 of all the pearls. The following describes how to compare a pair of pearls:
We can give you a balance to compare the weight of the Pearl. We can compare the weight of the Two pearls. After a series of comparisons, we can take some pearls that certainly do not have intermediate weight.
For example, the following example shows how to compare 5 pearls four times:
1. Pearl 2 is heavier than Pearl 1
2. Pearl 4 is 3 heavier than Pearl
3. Pearl 5 is heavier than Pearl 1
4. Pearl 4 is twice as heavy as Pearl 2
Based on the above results, although we cannot accurately find out which Pearl has intermediate weight, we can be sure that pearl 1 and pearl 4 cannot have intermediate weight, because Pearl 2, 4, 5 is heavier than Pearl 1, and pearl 1, 2, 3 is lighter than Pearl 4, we can remove these two pearls.
Write a program to calculate the total number of pearls.
Input/Output Format
Input description:
The first line of the input file contains two integers N and M separated by spaces, where 1 <= N <= 99, and N is an odd number. M indicates the number of pearl comparisons, each row in the next M line contains two integers x and y separated by spaces, indicating that pearl x is heavier than Pearl y.
Output description:
Only one row of the output file contains an integer, indicating the total number of pearls with intermediate weights.
Input and Output sample
Input example #1:
5 42 14 35 14 2
Output sample #1:
2
Ideas
Input data first, then list the size of each number based on the comparison, and finally output the number of impossible pearls.
Code # include <stdio. h> int a [100] [100], B [3] [100] = {0}; int main () {int n, I, m, j, k = 0, s; scanf ("% d", & n, & m); for (I = 1; I <= m; I ++) for (j = 1; j <= 2; j ++) scanf ("% d", & a [I] [j]); s = n/2 + 1; for (I = 1; I <= m; I ++) {B [a [I] [1] [1] = a [I] [1]; B [a [I] [1] [3] ++; B [a [I] [2] [1] = a [I] [2]; B [a [I] [2] [2] ++;} for (I = 1; I <= m; I ++) {B [a [I] [1] [3] = B [a [I] [1] [3] + B [a [I] [2] [3]; B [a [I] [2] [2] = B [a [I] [2] [2] + B [a [I] [1] [2 ];} for (I = 1; I <= n; I ++) if (B [I] [2]> = s | B [I] [3]> = s) k ++; printf ("% d", k); return 0 ;}View Code