7-277 单身狗 (25 分)

7-277 单身狗 (25 分)

“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。

输入格式:

输入第一行给出一个正整数 N(≤50000),是已知夫妻/伴侣的对数;随后 N 行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个 ID 号,为 5 位数字(从 00000 到 99999),ID 间以空格分隔;之后给出一个正整数 M(≤10000),为参加派对的总人数;随后一行给出这 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

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	int i,N,N0;
	string input,output="";
	cin >> N;
	N0 = N;
	int*a = new int[N];
	for (i = 0; i < N; i++)
		a[i] = 0;
	while (N--)
	{
		cin >> input;
		for (i = 0; i < input.length(); i++)
			a[N0 - N - 1] += (input[i]-48);
	}
	sort(a,a+N0);
	output += to_string(a[0]) + " "; 
	N = 1;
	for(i=1;i<N0;i++)
		if (a[i] != a[i - 1])
		{
			N++;
			output += to_string(a[i]) + " ";
		}
	cout << N << endl;
	cout << output.substr(0,output.length()-1);
	return 0;
}

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;

int main()
{
    int n;
    cin >> n;
    map<int, int> Map;//存储每对夫妻
    for(int i=0; i<n; i++){
        int a,b;
        scanf("%05d %05d", &a, &b);
        Map[a] = b;
        Map[b] = a;
    }
    int m;
    cin >> m;
    int arr[100000] = {0};
    for(int i=0; i<m; i++){
        int temp;
        scanf("%05d", &temp);
        arr[temp] = 1;//来参加派对的人为1
    }
    vector<int> res;
    for(int i=0; i<100000; i++){
        if(arr[i]!=0 && arr[Map[i]]==0){//自己来参加派对的人并且对象没来
            res.push_back(i);
        }
    }
    printf("%d\n", res.size());
    for(int i=0; i<res.size(); i++){
        if(i!=0)
            printf(" ");
        printf("%05d", res[i]);
    }
    return 0;
}
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	string a,b;
	cin>>n;
	map<string,string>m;
	set<string>s;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		m[a]=b;
	}
	int M;
	cin>>M;
	for(int i=1;i<=M;i++)
	{
		cin>>a;
		s.insert(a);
	}
	for(auto it=m.begin();it!=m.end();it++)
	{
		if(s.find(it->first)!=s.end()&&s.find(it->second)!=s.end())
		{s.erase(it->first);s.erase(it->second);}
	}
	cout<<s.size()<<endl;
	for(auto it=s.begin();it!=s.end();it++)
	{
		if(it==s.begin()) cout<<*it;
		else cout<<" "<<*it;
	}
	return 0;
}
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
using namespace std;

int main(){ 
    int n;
    cin >> n;
    vector<int> couple(100000);
    for (int i=0; i<n; i++){
        int a, b;
        cin >> a >> b;
        couple[a] = b+1;
        couple[b] = a+1;
    }

    int m;
    cin >> m;
    set<int> ss;
    set<int>::iterator it;
    for (int i=0; i<m; i++){
        int a;
        cin >> a;
        if (couple[a]!=0 && ss.find(couple[a]-1)!=ss.end()){
            ss.erase(ss.find(couple[a]-1));
        }
        else {
            ss.insert(a);
        }
    }

    //set会自动排序(从小到大)
    cout << ss.size() << endl;
    if (ss.size()>0){
        it = ss.begin();
        cout << setw(5) << setfill('0') << *it++;
        while (it!=ss.end()){
            cout << " " << setw(5) << setfill('0') << *it++;
        }
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51916951/article/details/121092617
今日推荐