PAT 甲級(jí) 刷題日記|A 1121 Damn Single (25 分)

思路

給N對(duì)夫妻編號(hào),再給M個(gè)派對(duì)里的參與人的編號(hào),輸出單身的人的編號(hào)(包括夫妻沒(méi)全部到場(chǎng)的也算單身)

代碼

#include <bits/stdc++.h>
using namespace std;

unordered_map<int, int> couple;
int n, p;
vector<int> las;
int flag[100002];
vector<int> ans;

int main() {
    cin>>n;
    for (int i = 0; i < 2 * n; i++) {
        int num;
        cin>>num;
        couple[num] = i / 2 + 1;
    }
    cin>>p;
    for (int i = 0; i < p; i++) {
        int peo;
        cin>>peo;
        if (couple.find(peo) == couple.end()) {
            ans.push_back(peo);
        } else {
            las.push_back(peo);
            int n = couple[peo];
            flag[n]++;
        }
    }
    int t = las.size();
    for (int i = 0; i < t; i++) {
        int now = couple[las[i]];
        if (flag[now] == 1)
            ans.push_back(las[i]);
    }
    sort(ans.begin(), ans.end());
    int s = ans.size();
    cout<<s<<endl;
    for (int i = 0; i < s; i++) {
        printf("%05d", ans[i]);
        if (i != s - 1) cout<<" ";
        else cout<<endl;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容