1. 程式人生 > >PAT Basic 1065

PAT Basic 1065

htm namespace lang define -o bottom mes cstring num

1065 單身狗

“單身狗”是中文對於單身人士的一種愛稱。本題請你從上萬人的大型派對中找出落單的客人,以便給予特殊關愛。

輸入格式:

輸入第一行給出一個正整數 N(≤ 50 000),是已知夫妻/伴侶的對數;隨後 N 行,每行給出一對夫妻/伴侶——為方便起見,每人對應一個 ID 號,為 5 位數字(從 00000 到 99999),ID 間以空格分隔;之後給出一個正整數 M(≤ 10 000),為參加派對的總人數;隨後一行給出這 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

  題解:這道題其實用map非常方便,我當時硬生生把寫出來了。

代碼如下:
 1 #include<iostream>
 2 #include<cstring>
 3 #define N 100000
 4 
 5 using namespace std;
 6 int ac[N] = {0}, result[N] = {0}, result2[N];
7 8 int main() 9 { 10 int n, a, b, m, num = 0, max = 0, min = N+1; 11 int max2 = 0, min2 = N+1, ok = 0; 12 memset(ac,-1, N); 13 memset(result,-1, N); 14 memset(result,-1, N); 15 scanf("%d",&n); 16 while(n--){ 17 scanf("%d %d",&a,&b); 18 ac[a] = b;
19 ac[b] = a; 20 if( a > max2 ) max2 = a; 21 if( b > max2 ) max2 = b; 22 if( a < min2 ) min2 = a; 23 if( b < min2 ) min2 = b; 24 } 25 scanf("%d",&m); 26 while( m--){ 27 scanf("%d",&a); 28 result[a] = 1; 29 if(a > max) max = a; 30 if( a < min ) min = a; 31 } 32 for( int i = min; i <= max; i++){ 33 if( result[i] != 1) continue; 34 else if( ac[i] == -1 ){ 35 result2[num] = i; 36 num++; 37 } 38 else if( result[ac[i]] != 1 ){ 39 result2[num] = i; 40 num++; 41 } 42 } 43 printf("%d\n",num); 44 for( int i = 0; i < num; i++){ 45 if(!ok) 46 ok = 1; 47 else 48 printf(" "); 49 if(result2[i] < 10) 50 printf("0000"); 51 else if( result2[i] < 100) 52 printf("000"); 53 else if( result2[i] < 1000) 54 printf("00"); 55 else if( result2[i] < 10000) 56 printf("0"); 57 printf("%d",result2[i]); 58 } 59 return 0; 60 }

 

PAT Basic 1065