2020年PAT乙级春考B-5 1115裁判机 题型:大模拟 独立完成

90 阅读1分钟

1115 裁判机 - PAT (Basic Level) Practice (中文) (pintia.cn)

思路

直接看代码吧

#include<bits/stdc++.h>
using namespace std;
const int N=12;
const int M = 1e3 + 10;
set<int>s;
int s2[M*M];
vector<int>v;
int flag[N];
int a[N][M];

void Sub(int x)
{
	for (int i = 0; i < v.size(); i++)
	{
		int sub = abs(x - v[i]);
		s.insert(sub);
	}
}

int main()
{
	cin.tie(nullptr)->sync_with_stdio(false);
	int x1, x2; cin >> x1 >> x2;
	v.push_back(x1), v.push_back(x2);
	s2[x1] = 1, s2[x2] = 1;
	s.insert(abs(x1 - x2));

	int n, m; cin >> n >> m;  //n行m列


	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cin >> a[i][j];
		}
	}

	//按列遍历
	for (int i = 1; i <= m; i++)  //列
	{
		for (int j = 1; j <= n; j++) //行
		{
			int x = a[j][i];
			if (s.count(x) && flag[j] == 0 && s2[x] == 0)
			{
				Sub(x);
				v.push_back(x);
				s2[x] = 1;
	
			}
			else
			{
				if (flag[j] == 0)//j != x&&y!=i)
				{
					cout << "Round #" << i << ": " << j << " is out." << endl;
					//淘汰之后打个标记,这个人以后就不参与游戏了
					flag[j] = 1;  //一行是一个人,标记一下和这行即可
				}

			}
		}
	}



	int cnt = 0;
	for (int i = 1; i <= n; i++)
	{
		if (flag[i] == 0)cnt++;
	}
	if (cnt >= 1)
	{
		cout << "Winner(s):";
		for (int i = 1; i <= n; i++)
		{
			if (flag[i] == 0)
			{
				cout << " " << i;
			}
		}
	}
	else
	{
		cout << "No winner." << endl;
	}

	return 0;
}

image.png