PAT——1065. 单身狗

“单身狗”是中文对于单身人士的一种爱称。本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱。

输入格式:

输入第一行给出一个正整数N(<=50000),是已知夫妻/伴侣的对数;随后N行,每行给出一对夫妻/伴侣——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(<=10000),为参加派对的总人数;随后一行给出这M位客人的ID,以空格分隔。题目保证无人重婚或脚踩两条船。

输出格式:

首先第一行输出落单客人的总人数;随后第二行按ID递增顺序列出落单的客人。ID间用1个空格分隔,行的首尾不得有多余空格。

输入样例:

3
11111 22222
33333 44444
55555 66666
7
55555 44444 10000 88888 22222 11111 23333

输出样例:

5
10000 23333 44444 55555 88888

 1 import java.util.Scanner;
 2 public class Main {
 3 
 4     public static void main(String[] args) {
 5         int N= 100000;
 6         Scanner in = new Scanner(System.in);
 7         int n = in.nextInt();
 8         int[] hasbandWife = new int[N];
 9         for (int i = 0; i < n; i++) {
10             int a = in.nextInt();            
11             int b = in.nextInt();            
12             hasbandWife[a] = b;                
13             hasbandWife[b] = a;
14         }
15         int m = in.nextInt();                
16         int[] test = new int[m];
17         for (int i = 0; i < m; i++) {
18             test[i] = in.nextInt();
19         }
20         
21         for (int i = 0; i < m; i++) {
22             if (hasbandWife[test[i]] == 0) {
23                 hasbandWife[test[i]]=-1;            
24             }
25             else if(hasbandWife[test[i]]==-1) {
26                 
27             }
28             else if(hasbandWife[test[i]]==1){
29                 
30             }
31             else {
32                 for (int j = i+1; j < m; j++) {
33                     if (hasbandWife[test[i]]==test[j]) {
34                         hasbandWife[test[i]] = 1;        
35                         hasbandWife[test[j]] = 1;            
36                         break;
37                     }
38                 }
39                 if (hasbandWife[test[i]]!=1) {            
40                     hasbandWife[test[i]]=-1;
41                 }
42             }
43         }
44         
45         int cnt = 0;
46         for (int i = 0; i < N; i++) {
47             if (hasbandWife[i]==-1) {
48                 cnt++;
49             }
50         }
51         
52         System.out.println(cnt);
53         
54         int flag =1;  
55         for(int i=0 ;i<N ;i++){  
56             if(hasbandWife[i]==-1){
57                 if(flag == 1){                
58                     flag=0;  
59                     System.out.print(i);
60                 }else 
61                     System.out.print(" "+i);  
62             }  
63         }  
64     }
65 }



原文地址:https://www.cnblogs.com/xiaxj/p/8005461.html