PAT乙级1065 单身狗 (25 分)测试用例

本文通过一个具体的C++程序示例介绍了如何实现一种基于输入数据的匹配算法。该算法能够处理多个输入对,并从中筛选出符合条件的数据进行输出。文章详细解释了程序的主要逻辑和关键步骤。

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

此题巨坑
https://pintia.cn/problem-sets/994805260223102976/problems/994805266942377984
测试点2测试用例

输入
1
00000 11111
2
00000 11111

输出
0

测试用例
输入
1
11111 22222
1
00000
输出
1
00000
#include <iostream>
#include <algorithm>
using namespace std;
int match[100010] = {0}, danshen[10010] = {0}, present[100010] = {0}, result[100010] = {0};
int main(){
	int n, k, cnt = 0, tmp;
	fill(match, match+100010, -1);
	cin >> n;
	for(int i = 0; i < n; i++){
		int tmp1, tmp2;
		cin >> tmp1 >> tmp2;
		match[tmp1] = tmp2;
		match[tmp2] = tmp1;
	}
	cin >> k;
	for(int i = 0; i < k; i++){
		cin >> tmp;
		present[tmp] = 1;
		danshen[i] = tmp;
	}
	for(int i = 0; i < k; i++){
		tmp = danshen[i];
		if(match[tmp] == -1)
			result[cnt++] = tmp;
		else{
			if(!present[match[tmp]])
				result[cnt++] = tmp;
		}
	}
	sort(result, result+cnt);
	printf("%d\n", cnt);
	if(cnt)	printf("%05d", result[0]);
	for(int i = 1; i < cnt; i++)
		printf(" %05d", result[i]);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值