UVa665 - False coin(枚举法)

 In order to detect the false coin the bank employees numbered all coins by theintegers from 1 to N, thus assigning each coin a unique integer identifier.After that they began to weight various groups of coins by placing equalnumbers of coins in the left pan and in the right pan. The identifiers ofcoins and the results of the weightings were carefully recorded.

You are towrite a program that will help the bank employees to determine the identifierof the false coin using the results of these weightings.

Input 

The first line of the input is an integer M, then a blank line followed by M datasets. There is a blank line between datasets.

The first line of each dataset contains two integers N and K, separatedby spaces, where N is the number of coins ()and K is thenumber of weightings fulfilled (). The following 2K linesdescribe all weightings. Two consecutive lines describe each weighting. Thefirst of them starts with a number Pi (), representing the numberof coins placed in the left and in the right pans, followed by Piidentifiers of coins placed in the left pan and Pi identifiers of coinsplaced in the right pan. All numbers are separated by spaces.

The second linecontains one of the following characters: `<', `>', or `='.It represents the result of the weighting:

  • `<' means that the weight of coinsin the left pan is less than the weight of coins in the right pan,
  • `>' meansthat the weight of coins in the left pan is greater than the weight of coinsin the right pan,
  • `=' means that the weight of coins in the left pan isequal to the weight of coins in the right pan.

Output 

For each dataset, write to the output file the identifier of the false coin or 0, if itcannot be found by the results of the given weightings. Print a blank line between datasets.

Sample Input 

1

5 3
2 1 2 3 4
<
1 1 4
=
1 2 5
=

Sample Output 

3

题意:根据稳重的结果判断哪个是假币,如果无法判断,就输出0

思路:采用枚举法,从1到n,枚举第i个假币轻或者重,如果发现满足条件的只有一个,说明可以判断出,否则说明无法判断出哪个是假币

#include <cstdio>

using namespace std;

const int MAXN = 110;

struct Weight
{
	int l[MAXN], r[MAXN], c;
};

Weight w[MAXN];
int n, k;

bool input()
{
	scanf("%d%d", &n, &k);
	
	for (int i = 0; i < k; i++) {
		int p;
		scanf("%d", &p);
		for (int j = 0; j < p; j++) {
			scanf("%d", &w[i].l[j]);
		}
		w[i].l[p] = -1;
		
		for (int j = 0; j < p; j++) {
			scanf("%d", &w[i].r[j]);
		}
		w[i].r[p] = -1;
		
		char buf[2];
		scanf("%s", buf);
		if (buf[0] == '<') w[i].c = -1;
		else if (buf[0] == '>') w[i].c = 1;
		else w[i].c = 0;
	}

	return true;
}

bool test(int c, int m)
{
	int s, t;
	
	for (int i = 0; i < k; i++) {
		s = 0;
		for (int j = 0; w[i].l[j] > 0; j++) {
			if (w[i].l[j] == c) s += m;
		}
		
		t = 0;
		for (int j = 0; w[i].r[j] > 0; j++) {
			if (w[i].r[j] == c) t += m;
		}
		
		if (s < t && w[i].c >= 0) return false;
		else if (s > t && w[i].c <= 0) return false;
		else if (s == t && w[i].c != 0) return false;
	}
	
	return true;
}

void solve()
{
	int i;
	
	int ans = 0;
	int coin;
	
	for (i = 1; i <= n; i++) {
		if (test(i, -1)) {
			ans++;
			coin = i;
		} else if (test(i, 1)) {
			ans++;
			coin = i;
		}
	}
	
	if (ans != 1) printf("0\n");
	else printf("%d\n", coin);
}

int main(int argc, char **argv)
{
#ifndef ONLINE_JUDGE
	freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

	int cas;
	scanf("%d", &cas);
	while (cas--) {
		input();
		solve();
		if (cas) printf("\n");
	}

	return 0;
}



你可能感兴趣的:(UVa665 - False coin(枚举法))