PAT (Basic Level) Practice (中文)1065 单身狗(C语言)
2019-01-16 09:12
477 查看
程序员入门水平,贴出代码大家一起进步!
题目
“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。
输入格式:
输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣的对数;随后 N 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数 M(≤ 10 000),为参加派对的总人数;随后一行给出这 M 位客人的 ID,以空格分隔。题目保证无人重婚或脚踩两条船。
输出格式:
首先第一行输出落单客人的总人数;随后第二行按 ID 递增顺序列出落单的客人。ID 间用 1 个空格分隔,行的首尾不得有多余空格。
输入样例:
3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333
输出样例:
5
10000 23333 44444 55555 88888
思路||总结
- 设置一个长度为10000行×2列的数组(int num[10000][2]),假设一对夫妻的编号分别为a、b,在自己的编号行中存放伴侣的编号,即将a放在num[b-1][0]中,将b存放在num[a-1][0]。
- 将测试的数据输入并进行排序,在num[i][1]中进行是否存在的标记,最后对于一个标号为a的顾客,找出其伴侣是否存在(这个地方在代码中不太好理解),如果不存在则输出。
AC代码
#include <stdio.h> #include <stdlib.h> int main() { int comp(const void *a,const void *b); int i, n,t; scanf("%d",&n); int cp[100000][2]={0}; for(i=0;i<n;i++)//夫妻信息临时存放在t1,t2中,然后在第t1行中存放t2,在t2行中存放t1。 { int t1,t2; scanf("%d %d",&t1,&t2); cp[t1][0]=t2; cp[t2][0]=t1; }; int m; scanf("%d",&m); int custom[m]; for(i=0;i<m;i++) { scanf("%d",&custom[i]); cp[custom[i]][1]=1; }; qsort(custom,m,sizeof(int),comp);//将客户编号排序。 int tem=m; for(i=0;i<m;i++) if(cp[cp[custom[i]][0]][1]==1)tem--;//这个地方理解起来比较麻烦。 printf("%d\n",tem); for(i=0;i<m;i++) { if(cp[cp[custom[i]][0]][1]==0) { printf("%05d",custom[i]); if(i<m-1)putchar(' '); }; }; return 0; } int comp(const void *a,const void *b) {
相关文章推荐
- PAT (Basic Level) Practice (中文)1072 开学寄语(C语言)
- PAT (Basic Level) Practice (中文)1069 微博转发抽奖(C语言)
- PAT (Basic Level) Practice (中文)1088 三人行(C语言)
- PAT (Basic Level) Practice (中文)1070 结绳(C语言)
- PAT (Basic Level) Practice (中文)1077 互评成绩计算(C语言)
- PAT (Basic Level) Practice (中文)1080 MOOC期终成绩(C语言)
- PAT (Basic Level) Practice (中文)1089 狼人杀-简单版(C语言)
- PAT (Basic Level) Practice (中文)1066 图像过滤(C语言)
- PAT (Basic Level) Practice (中文)1081 检查密码(C语言)
- PAT (Basic Level) Practice (中文)1085 PAT单位排行(C语言)
- PAT (Basic Level) Practice (中文)1086 就不告诉你(C语言)
- PAT (Basic Level) Practise (中文) 1065. 单身狗(25)
- PAT (Basic Level) Practice (中文)1075 链表元素分类(C语言)
- PAT (Basic Level) Practice (中文)1082 射击比赛(C语言)
- PAT (Basic Level) Practice (中文)1087 有多少不同的值(C语言)
- PAT (Basic Level) Practice (中文)1067 试密码(C语言)
- PAT (Basic Level) Practice (中文)1078 字符串压缩与解压(C语言)
- PAT (Basic Level) Practice (中文)1079 延迟的回文数(C语言)
- PAT (Basic Level) Practice (中文)1073 多选题常见计分法 (C语言)
- PAT (Basic Level) Practice (中文)1074 宇宙无敌加法器(C语言)