1065 单身狗 (25 分) java 使用数据结构超时
作者:互联网
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class text1065true {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<Integer, Integer> couple = new HashMap<>();
int n = in.nextInt();
while (n-- > 0) {
int key = in.nextInt();
int value = in.nextInt();
couple.put(key, value);
}
Set<Entry<Integer, Integer>> s = couple.entrySet();
ArrayList<Integer> alone = new ArrayList<>();
ArrayList<Integer> test = new ArrayList<>();
n = in.nextInt();
while (n-- > 0) {
test.add(in.nextInt());
}
for (int i = 0; i < test.size(); i++) {
int a = test.get(i);
boolean boy = couple.containsKey(a);
boolean girl = couple.containsValue(a);
boolean flag = false;
Integer wife = 0;
if (boy) {
for (Entry<Integer, Integer> vo : couple.entrySet()) {
if (vo.getKey() == a) {
wife = vo.getValue();
break;
}
}
if (test.contains(wife)) {
flag = true;
}
}
if (girl) {
for (Entry<Integer, Integer> vo : couple.entrySet()) {
if (vo.getValue() == a) {
wife = vo.getKey();
break;
}
}
if (test.contains(wife)) {
flag = true;
}
}
if (!flag) {
alone.add(a);
}
}
Object er[] = alone.toArray();
Arrays.sort(er);
System.out.println(er.length);
for (int i = 0; i < er.length - 1; i++) {
System.out.print(er[i] + " ");
}
System.out.print(er[er.length - 1]);
}
}
标签:25,java,int,couple,1065,vo,test,er 来源: https://blog.csdn.net/DavidSharch/article/details/100925564