7-4 find the element that is not a common two array (20 points)
Given two arrays of integers, the subject asks to find elements that are not common to both.
Input format:
The input gives two integer arrays in two rows, each giving a positive integer n (≤20), followed by n integers, separated by a space.
Output format:
Output in a row in the order given by the number is not an element common to the two array, and the numbers are separated by a space, but there is no extra space at the end of the line. The title guarantees that there is at least one such number. The same number does not repeat the output.
Input Sample:
10 3 -5 2 8 0 3 5 -15 9 10011 6 4 8 2 6 -5 9 0 100 8 1
Sample output:
3 5 -15 6 4 1
Idea: As a C + + STL practice record, just start to see the output is not sorted, all with unordered_set, but found that this container is really disorderly! However, the input sequence is not all or needs to be saved by an array in the order of entry.
There is a merger that seems to be not very practical, the most cost-effective or find ();
#include <iostream>#include<cstdio>#include<vector>#include<string>#include<unordered_set>#include<algorithm>using namespacestd;intMain () {unordered_set<int>SE1, Se2; inta[ -], b[ -]; intN1, N2, Num; CIN >>N1; for(inti =0; I < N1; i++) {cin>>num; A[i]=num; Se1.insert (num); } CIN>>N2; for(inti =0; I < N2; i++) {cin>>num; B[i]=num; Se2.insert (num); } unordered_set<int>:: Iterator iter; intFlag =1; for(inti =0; I < n1;i++){ if(Se2.find (a[i])! =se2.end ())Continue; if(Flag = =1) {cout << a[i]; flag =0; } Elsecout <<" "<<A[i]; Se2.insert (A[i]); } for(inti =0; I < n2;i++){ if(Se1.find (b[i])! =se1.end ())Continue; if(Flag = =1) {cout << b[i]; flag =0; } Elsecout <<" "<<B[i]; Se1.insert (B[i]); } if(Flag! =1) cout <<Endl; return 0;}
7-4 find the element that is not a common two array (20 points)