当前位置: 代码迷 >> 综合 >> PAT(甲级)2020年春季考试 7-2 The Judger (25分)
  详细解决方案

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

热度:65   发布时间:2024-02-21 14:27:39.0

前言

在这里插入图片描述
参考题解

//
// Created by niko on 2020/9/22.
//#include <bits/stdc++.h>using namespace std;
/*** 题意:对于一个数字游戏,输出出局的人在哪一轮出局,以及最终的获胜者* 思路:见代码注释* @return*/
const int N = 12;
const int M = 1e3 + 10;
int n, m, a[N][M];//a数组存储输入
unordered_set<int> origin;//origin记录有效的number;
bool check(int x){
    //查找是否是前面元素的差for (auto it:origin) {
    if (origin.find(it + x) != origin.end()) {
    return true;}}return false;
}
int main() {
    int t1, t2;scanf("%d %d", &t1, &t2);origin.insert(t1);origin.insert(t2);scanf("%d %d", &n, &m);vector<int> remain;for (int i = 1; i <= n; i++) {
    remain.push_back(i);for (int j = 1; j <= m; ++j) {
    scanf("%d", &a[i][j]);}}//j表示第几轮,i表示用户ifor (int j = 1; j <= m; ++j) {
    vector<int> out;//每一轮出局的人的Idfor (int i = 0,l=remain.size(); i < l; ++i) {
    int id = remain[i];if (origin.find(a[id][j]) != origin.end()||!check(a[id][j])) {
    out.push_back(id);printf("Round #%d: %d is out.\n", j, id);} else{
    //满足题意origin.insert(a[id][j]);}}//删除那些已经出局的人for (int k = 0; k < out.size(); ++k) {
    remain.erase(find(remain.begin(), remain.end(), out[k]));}}int cnt = remain.size();if(cnt==0)printf("No winner.\n");else{
    printf("Winner(s):");for (auto it:remain) {
    printf(" %d", it);}}return 0;
}
  相关解决方案