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 ($1 \le N \le 100$)and K is thenumber of weightings fulfilled ($1 \le K \le 100$). The following 2K linesdescribe all weightings. Two consecutive lines describe each weighting. Thefirst of them starts with a number Pi ($1 \le P_i \le N/2$), 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值