PAT L1-020. 帅到没朋友 集合set瞎搞

L1-020. 帅到没朋友

当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。

输入格式:

输入第一行给出一个正整数N(<=100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(<=1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(<=10000),为待查询的人数;随后一行中列出M个待查询的ID,以空格分隔。

注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过1的朋友圈里都至少有2个不同的人。

输出格式:

按输入的顺序输出那些帅到没朋友的人。ID间用1个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出“No one is handsome”。

注意:同一个人可以被查询多次,但只输出一次。

输入样例1:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
输出样例1:
10000 88888 23333
输入样例2:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
输出样例2:
No one is handsome

题目链接:https://www.patest.cn/contests/gplt/L1-020

题解:

    用一个集合来保证每个朋友圈里不会重复添加人,将集合中的id对应数组cnt++,但是要注意一个朋友圈里只有一个人的时候不能++;在查询输出时,要对已经输出过的做标记used[maxn]避免重复输出

//#include <bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<sstream>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const int maxn = 100005;
int cnt[maxn];
int used[maxn];
set<int> s;

int main(){
    int n;
    while(scanf("%d", &n) == 1 &&n){
        memset(cnt, 0, sizeof(cnt));
        memset(used, 0, sizeof(used));
        for(int i=0; i<n; i++){
            s.clear();
            int k;
            scanf("%d", &k);
            while(k--){
                int id;
                scanf("%d", &id);
                s.insert(id);
            }
            if(s.size() == 1){
                continue;
            }
            for(set<int>::iterator it=s.begin(); it!=s.end(); it++){
                cnt[*it]++;
            }
        }
        int q;
        scanf("%d", &q);
        int inc = 0;
        while(q--){
            int id;
            scanf("%d", &id);
            if(!used[id] && cnt[id] == 0){
                if(inc>0) printf(" ");
                printf("%05d", id);
                used[id] = 1;
                inc++;
            }
        }
        if(inc == 0){
            printf("No one is handsome");
        }
        printf("\n");
    } 
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值