1. 程式人生 > >PAT乙級真題 1065. 單身狗(25)

PAT乙級真題 1065. 單身狗(25)

***還是比較簡單的,問題在於最後輸出時如果是4位數,應當輸出前置的0,這一點不注意的話,有一個點始終過不去*。**
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <iomanip>
using namespace std;

int main()
{
    map<int,int> A;
    int N;
    cin >> N;
    //這是夫妻的map
    for (int i= 0
; i < N; i++) { int f,q; cin >> f>>q; A.insert({ f,q }); } int M; cin >> M; vector<int> B; for (int i = 0; i < M; i++) { int num; cin >> num; B.push_back(num); } vector<int> C; for
(int i = 0; i < B.size(); i++) { if (A.find(B[i]) != A.end()) { if(find(B.begin(),B.end(), A.at(B[i]))!=B.end()) { C.push_back(B[i]); C.push_back(A.at(B[i])); } } } sort(B.begin(), B.end()); sort(C.begin(), C.end()); vector
<int>
D(B.size()-C.size()); set_difference(B.begin(), B.end(), C.begin(), C.end(), D.begin()); sort(D.begin(), D.end()); cout << D.size()<<endl; for (int i = 0; i < D.size(); i++) { //printf("%05d", D[i]); cout<< setfill('0'); cout << setw(5) << D[i]; if (i != D.size() - 1) cout << " "; } return 0; }