Title Description
give you a balance to compare the weight of a pearl, We can compare two pearls which are heavier, and after making a series of comparisons, we can take away some pearls that certainly do not have the middle weight.
1, Pearl 2 than Pearl 1 weight
2, Pearl 4 than Pearl 3 weight
3, pearl 5 than Pearl 1 weight
4, pearl 4 than Pearl 2 weight
write a program that counts how many pearls are definitely not the middle weight.
input/output format
Input Description:
The first line of the input file contains two spaces separated by integers n and M, where 1<=n<=99, and N is odd, M is the number of comparisons to pearls, and the next m row contains two integers separated by spaces X and Y, indicating that Pearl X is heavier than Pearl Y.
Output Description:
The output file contains only one line with an integer representing the total number of pearls that cannot be the middle weight.
input/Output sample
Input Sample # #:
5 42 14 35 14 2
Sample # # of output:
2
Ideas
Enter the data first, then the size of each number according to the comparison, and finally the number of impossible pearls output.
Code
#include <stdio.h>inta[ -][ -],b[3][ -]={0};intMain () {intn,i,m,j,k=0, S; scanf ("%d%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
Pearl (Bead)