1029 False coin

Description
"bars" bank to obtain information from reliable sources, in their last set of N coins, a coin is just false coins and different from the other weight (equal to the weight of all the other coin). After the economic crisis, they have only a simple balance point (a figure). With this balance, one can determine the weight of the object is left in the pan is less than, greater than or equal to the weight of the object in the right pan.
To detect false coin, bank employees by an integer from 1 to N numbered all the coins to dispense a unique integer identifier for each coin. Then, they started by placing the same number of coins in the left and right pan pan weighted various coin sets. Carefully record the results of the coin identifier and the right weight.
You will write a program to help bank employees use these weights the results to determine the false coin identifier.
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 ), K is the number of weights satisfy a weight (1 <= K <= 100 )) . The following line describes 2K re-ownership. Describe two successive rows each weight. The first one of them starts with number Pi (1 <= Pi <= N / 2), represents the number of coins placed on the left and right sides of the pan, followed by placing the pan on the left side of the coin Pi Pi identifier and an identifier coins in the pot right. All numbers are separated by spaces. The second row contains one of the following characters: '<', '>' or '='. It weight representation results:
'<' indicates a weight less than the weight of the coin pan left and right coin pot,
'>' represents the weight of the coin pan left and right pan is greater than the weight of the coins,
and '=' left flat pot coin weight by weight equal to the right coin pan.
Output
If you can not find the weight given by the result, the output file will be written fake coin identifier or 0.

Sample input

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

Sample Output

3
#include<iostream> 
#include<cstring>
#include<string>
using namespace std;

int n, k;
int pl[505],pr[505];
int a[1005],b[1005];

int main()
{
    while (cin >> n >> k)
    {
        int t;
        int num = 0;
        char c;
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        while (k--)
        {
            cin >> t;
            for (int i = 0; i < t; i++)
                cin >> pl[i];
            for (int i = 0; i < t; i++)
                cin >> pr[i];
            cin >> c;
            if (c == '=')
            {
                for (int i = 0; i < t; i++)
                {
                    a[pl[i]] = b[pl[i]] = -1;
                    a[pr[i]] = b[pr[i]] = -1;
                }
            }
            else if (c == '<')
            {
                num++;
                for (int i = 0; i < t; i++)
                {
                    if (a[pl[i]] != -1)   a[pl[i]]++;
                    if (a[pr[i]]!= -1)      b[pr[i]]++;
                }
            }
            else if (c == '>')
            {
                num++;
                for (int i = 0; i < t; i++)
                {
                    if (a[pl[i]] != -1)   b[pl[i]]++;
                    if (a[pr[i]] != -1)      a[pr[i]]++;
                }
            }
        }
        int ans, count=0;
        for (int i = 1; i <= n; i++)
        {
            if (a[i] == num || b[i]==num)
            {
                ans = i;
                count++;
            }
            if (count == 2)  break;
        }
        if (count!=1)  cout << "0" << endl;
        else cout << ans << endl;
    }
}

Source: https: //www.cnblogs.com/zyb993963526/p/6515377.html

Guess you like

Origin www.cnblogs.com/sweet-ginger-candy/p/11518213.html