PAT 2020年春季 7-2 The Judger (25 分)

A game of numbers has the following rules: at the beginning, two distinct positive integers are given by the judge. Then each player in turn must give a number to the judge. The number must be the difference of two numbers that are previously given, and must not be duplicated to any of the existed numbers. The game will run for several rounds. The one who gives a duplicate number or even a wrong number will be kicked out.

Your job is to write a judger program to judge the players' numbers and to determine the final winners.

Input Specification:

Each input file contains one test case. For each case, the first line gives two distinct positive integers to begin with. Both numbers are in [1,10^5​​].

In the second line, two numbers are given: N (2≤N≤10), the number of players, and M (2≤M≤10^3​​), the number of rounds.

Then N lines follow, each contains M positive integers. The i-th line corresponds to the i-th player (i=1,⋯,N). The game is to start from the 1st player giving his/her 1st number, followed by everybody else giving their 1st numbers in the 1st round; then everyone give their 2nd numbers in the 2nd round, and so on so forth.

Output Specification:

If the i-th player is kicked out in the k-th round, print in a line Round #k: i is out.. The rest of the numbers given by the one who is out of the game will be ignored. If more than one player is out in the same round, print them in increasing order of their indices. When the game is over, print in the last line Winner(s): W1 W2 ... Wn, where W1 ... Wn are the indices of the winners in increasing order. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line. If there is no winner, print No winner. instead.

Sample Input 1:

101 42
4 5
59 34 67 9 7
17 9 8 50 7
25 92 43 26 37
76 51 1 41 40

Sample Output 1:

Round #4: 1 is out.
Round #5: 3 is out.
Winner(s): 2 4

Sample Input 2:

42 101
4 5
59 34 67 9 7
17 9 18 50 49
25 92 58 1 39
102 32 2 6 41

Sample Output 2:

Round #1: 4 is out.
Round #3: 2 is out.
Round #4: 1 is out.
Round #5: 3 is out.
No winner.

实现思路:

记得自己当初考试的时候就差这一道题,真的无奈留了一个半小时,硬生生看不懂题目,就败在了自己的垃圾英文,difference硬生生翻译成不同,可惜不知道它还有一个差的意思,血亏啊,本来就直接满分了。
题意就是系统先给出2个数字,然后要求如下:
一共N个玩家玩M轮游戏,每轮一个玩家给出一个数字,这个数字必须是之前所有已经出现的数字中的任2个数字的差,并且当前所给数字从未出现过,则该玩家该轮所给的才是合法数字,否则就是不合法的数字,该玩家将会被淘汰出局,淘汰出局的话,当前玩家该轮以及之后所给的数字都将忽略不计。
一看题目有重复数据,并且要做一个集合中任意两个数字的差,最好的办法就是利用一个set集合,将所有出现数字进行保存,再迭代set容器it,只需要把当前所给数字x加上it得到一个val值,如果set寻找val值是存在的说明,之前出现的数字里存在两数之差为x即可,另外准备一个set容器用于保存淘汰人的id,之后轮数中出现这个人则直接忽略。

AC代码:

#include <iostream>
#include <unordered_set>
#include <set>
using namespace std;
unordered_set<int>	st;
set<int> back;

int G[15][1010],n1,n2,n,m;

bool judge(int x) {
	if(st.find(x)!=st.end()) return false;//此前有该数存在重复了
	for(auto it : st) {
		if(st.find(it+x)!=st.end()) return true;//在容器中可以找到当前数和之前所给一个数的和
	}
	return false;
}

int main() {
	cin>>n1>>n2;
	st.insert(n1);
	st.insert(n2);
	cin>>n>>m;
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			scanf("%d",&G[i][j]);
		}
	}

	for(int i=0; i<m; i++) {
		for(int j=0; j<n; j++) {
			if(back.find(j)!=back.end()) continue;//如果此前这个人已经被淘汰则本轮忽略 
			int key=G[j][i];
			if(judge(key)) {
				st.insert(key);//是合法数字保存 
			} else {
				printf("Round #%d: %d is out.\n",i+1,j+1);
				back.insert(j);//保存淘汰人的id 
			}
		}
	}
	if(back.size()<n) {//当淘汰人数小于n时候有赢家 
		printf("Winner(s):");
		for(int i=0; i<n; i++)
			if(back.find(i)==back.end()) printf(" %d",i+1);
	} else printf("No winner.");
	return 0;
}
posted @ 2021-03-09 13:59  coderJ_ONE  阅读(112)  评论(0)    收藏  举报