False Coin
The "Gold Bar"bank received information from reliable sources that in their last group of N coins exactly one coin is false and differs in weight from other coins (while all other coins are equal in weight). After the economic crisis they have only a simple balance available (like one in the picture). Using this balance, one is able to determine if the weight of objects in the left pan is less than, greater than, or equal to the weight of objects in the right pan.In order to detect the false coin the bank employees numbered all coins by the integers from 1 to N, thus assigning each coin a unique integer identifier. After that they began to weight various groups of coins by placing equal numbers of coins in the left pan and in the right pan. The identifiers of coins and the results of the weightings were carefully recorded.You are to write a program that will help the bank employees to determine the identifier of the false coin using the results of these weightings.
Input
The first line of the input file contains two integers N and K, separated by spaces, where N is the number of coins (2<=N<=1000 ) and K is the number of weightings fulfilled (1<=K<=100). The following 2K lines describe all weightings. Two consecutive lines describe each weighting. The first of them starts with a number Pi (1<=Pi<=N/2), representing the number of coins placed in the left and in the right pans, followed by Pi identifiers of coins placed in the left pan and Pi identifiers of coins placed in the right pan. All numbers are separated by spaces. The second line contains one of the following characters: ‘<’, ‘>’, or ‘=’. It represents the result of the weighting:
‘<’ means that the weight of coins in the left pan is less than the weight of coins in the right pan,
‘>’ means that the weight of coins in the left pan is greater than the weight of coins in the right pan,
‘=’ means that the weight of coins in the left pan is equal to the weight of coins in the right pan.
Output
Write to the output file the identifier of the false coin or 0, if it cannot be found by the results of the given weightings.
Sample Input
5 3
2 1 2 3 4
‘<’
1 1 4
‘<’
1 2 5
'>'
Sample Output
3
暴力模拟:
思路:比较结果为“=”时,天平两边的砝码都是正常的。比较结果是“<”,或是“>”时,坏砝码肯定出现了,而且那个坏的砝码只能在一边(重的一边或者是轻的一边)。我们可以用一个book数组记录哪些砝码是好的,哪些还不能判断;用数组p记录砝码在天平的哪一边,如果在轻的那边p[i]–,否则p[i]++。因为坏砝码只能出现在“>”或“<”中,且只能出现在一边,因此坏砝码的p[坏]==±(“>”和“<”的出现次数)。
原文链接:https://blog.csdn.net/qq_41890797/article/details/81541082
代码:
#include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
int f[1010], book[1010], p[1010];
int main()
{
int n, m, k;
int ans = 0;
memset(f, 0, sizeof(f));
memset(book, 0, sizeof(book));
cin >> n >> k;
while (k--)
{
scanf("%d", &m);
for (int i = 0; i < 2 * m; i++)
scanf("%d", &p[i]);
getchar();
char c;
scanf("%c", &c);
if (c == '=')
for (int i = 0; i < 2 * m; i++)
book[p[i]] = 1;
else if (c == '<')
{
ans++;
for (int i = 0; i < m; i++) //轻的一边
f[p[i]]--;
for (int i = m; i < 2 * m; i++) //重的一边
f[p[i]]++;
}
else
{
ans++;
for (int i = 0; i < m; i++) //重的一边
f[p[i]]++;
for (int i = m; i < 2 * m; i++) //轻的一边
f[p[i]]--;
}
}
int ces = 0, flag;
for (int i = 1; i <= n; i++)
{
if (!book[i])
{
if (f[i] == ans || f[i] == -ans)
{
ces++;
flag = i;
}
}
}
if (ces == 1)
printf("%d\n", flag);
else
printf("0\n");
}
Black_xtree
发布了7 篇原创文章 · 获赞 0 · 访问量 69
私信
关注