Problem DescriptionGive two integers set a, B, and find their intersection, the set, and the remainder of B in a.
Input FormatThe first behavior is an integer n that represents the number of elements in collection A.
The second line has n distinct integers separated by spaces, representing the elements in collection A.
The third behavior is an integer m that represents the number of elements in collection B.
The four lines have m distinct integers separated by spaces, representing the elements in collection B.
All elements in the collection are integers in the range int, n, m<=1000.
output FormatThe first line outputs all elements of a, B intersection in order from small to large.
The second line prints all elements of a, B, and set in order from small to large.
The third line prints all the elements in the remainder set of B in a small to large order.
Sample Input
5 1 2 3 4 5 5 2 4 6 8 Ten
Sample output
2 4 1 2 3 4 5 6 8 Ten 1 3 5
Sample input
4 1 2 3 4 3 5 6 7
Sample output
1 2 3 4 5 6 7 1 2 3 4
Kam SAC 1
Post-order Processing.
Kam SAC 2
Sort first, for each set of operations, uses two pointers to point to the sorted collection, which is specially handled for the same element.
Java Test Code
1 Package cn.maxin.test;2 3 import java.util.ArrayList;4 import java.util.Collections;5 import java.util.List;6 import Java.util.Scanner;7 8 Public classMain {9 Ten Public Static voidMain (string[] args) { One AScanner sc =NewScanner (System.inch); - - intm =sc.nextint (); theList<integer> ListA =NewArraylist<integer>(); - for(inti =0; I < m; i++) { - Lista.add (Sc.nextint ()); - } + intn =sc.nextint (); -list<integer> Listb =NewArraylist<integer>(); + for(inti =0; I < n; i++) { A Listb.add (Sc.nextint ()); at } - sc.close (); - -list<integer> listu =NewArraylist<integer>(); - Listu.addall (LISTB); - Listb.retainall (ListA); in Lista.removeall (LISTB); - Listu.addall (ListA); to + Collections.sort (ListA); - Collections.sort (LISTB); the Collections.sort (listu); * $ for(inti =0; I < listb.size (); i++) {Panax NotoginsengSystem. out. Print (Listb.Get(i)); -System. out. print (i = = Listb.size ()-1?"\ r \ n":' '); the } + for(inti =0; I < listu.size (); i++) { ASystem. out. Print (Listu.Get(i)); theSystem. out. print (i = = Listu.size ()-1?"\ r \ n":' '); + } - for(inti =0; I < lista.size (); i++) { $System. out. Print (ListA.Get(i)); $System. out. print (i = = Lista.size ()-1?"\ r \ n":' '); - } - } the}
Algorithm Training set operation