PAT 天梯初赛 L1-4. 帅到没朋友

本题解析了一个趣味算法问题——找出帅到没朋友的人。通过输入朋友圈成员信息及待查询人员ID,程序判断哪些人不在任何朋友圈内,即被认为帅到没朋友。输入包含朋友圈数量、各朋友圈成员ID及待查询人员ID,输出则是帅到没朋友的人的ID列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                L1-4. 帅到没朋友

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

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

输入格式:

输入第一行给出一个正整数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
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int n,i,j,x,tem,hd[100005],m[100005],add;
int main()
{
    while (~scanf(" %d",&n))
    {
        memset(m,0,sizeof(m));
        for (i=0;i<n;i++)
        {
            scanf(" %d",&x);
            for (j=0;j<x;j++)
            {
                scanf(" %d",&tem);
                if (x>1) m[tem]=1;
            }
        }
        scanf(" %d",&n);
        memset(hd,0,sizeof(hd));
        add=0;
        for (i=0;i<n;i++)
        {
            scanf(" %d",&x);
            if (!m[x]&&!hd[x])
            {
                if (add) printf(" ");
                if (x<10000) printf("0");
                if (x<1000) printf("0");
                if (x<100) printf("0");
                if (x<10) printf("0");
                
                printf("%d",x);
                hd[x]++;
                add++;
            }
        }
        if (!add) printf("No one is handsome");
        printf("\n");
    }
    return 0;
}


### 解决方案 对于PAT L1-020 “朋友”的问题,目标是从给定的数据集中识别出那些有任何共同好友的人。具体实现可以通过集合操作来完成。 #### 数据结构的选择 为了高效处理这个问题,可以考虑使用Python中的`set`数据结构。通过将每个人的社交圈转换成集合,能够快速比较不同人的社交关系,并找出完全有交集的情况[^4]。 #### 实现逻辑 程序接收一系列整数表示人数以及每个人的朋友列表。接着遍历所有人际网络记录,利用集合运算检测是否有完全独立于其他任何群体之外的个体存在。如果有这样的情况发生,则收集这些特殊用户的编号;反之则返回预定义的消息“No one is handsome”。 下面是具体的代码实现: ```python def find_lonely_people(n, friend_lists): all_ids = set() not_alone = set() for friends in friend_lists: current_set = set(friends) all_ids.update(current_set) # Check intersection with existing sets to mark non-lonely people for other in friend_lists: if id(other) != id(friends): # Ensure we're comparing different lists intersected = current_set.intersection(set(other)) if intersected: not_alone.update(intersected) break lonely = sorted(list(all_ids.difference(not_alone))) result = ' '.join(lonely) if lonely else "No one is handsome" return result if __name__ == "__main__": n = int(input().strip()) friend_lists = [] while True: try: m = int(input().strip()) ids = list(map(int, input().strip().split())) friend_lists.append(ids) except EOFError: break print(find_lonely_people(n, friend_lists)) ``` 此段代码首先读取输入直到遇到文件结束符EOF(即所有测试用例都已给出),然后调用函数`find_lonely_people()`来进行实际计算并打印最终结果。注意这里的输入解析部分假设每组数据之间由空白行分隔开。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值