当前位置: 首页 > 工具软件 > judger > 使用案例 >

PAT(甲级)2020年春季考试7-2 The Judger (25 分)

颛孙铭
2023-12-01

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.

这个题用set会超时间,距离用s2[]存储, 值用unordered_set s1存储

#include<bits/stdc++.h>
using namespace std;
int start, end1;
int N, M, cnt = 0;
bool visit[15] = {false}, s2[100005];
vector<vector<int> > v(15, vector<int> (1005));
unordered_set<int> s1;
int main() {
	scanf("%d %d", &start, &end1);
	s1.insert(start);
	s1.insert(end1);
	s2[abs(start - end1)] = true;
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= M; j++){
			scanf("%d", &v[i][j]);
		}
	}
	for (int i = 1; i <= M; i++) {
		for (int j = 1; j <= N; j++) {
			if (visit[j]) continue;
			if (s1.find(v[j][i]) == s1.end() && s2[v[j][i]] == true){
				for (int it : s1) {
					s2[abs(it - v[j][i])] = true;
				}
				s1.insert(v[j][i]);
			} else {
				visit[j] = true;
				cnt++;
				printf("Round #%d: %d is out.\n", i, j);
			}
		}
	}
	if (cnt == N) {
		printf("No winner.");
	} else {
		printf("Winner(s):");
		for (int i = 1; i <= N; i++) {
			if (visit[i] == false) printf(" %d", i);
		}
	}
} 
 类似资料: