题目链接
点击打开链接
思路
定义couple数组,couple[i] == j,若j == 100001代表i没对象,否则i的对象为j。定义people数组,people[i] == j,若j == 0,则i没来聚会,若j == 1,则i来了。
代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(){
int couple_num,people_num,i,temp1_num,temp2_num;
int couple[100012], //couple[i] == j 代表i的对象是j
people[100012]={0}, //people[i] == 0 代表i没参加聚会
solo_num,temp_solo_num;
for(i = 0;i<100012;i++){//couple[i] == 100001代表i没对象
couple[i] = 100001;
}
char temp1[6],temp2[6];
scanf("%d",&couple_num);
for(i = 0;i< couple_num;i++){
scanf("%s %s",temp1,temp2);
temp1_num = atoi(temp1);
temp2_num = atoi(temp2);
couple[temp1_num] = temp2_num;
couple[temp2_num] = temp1_num;
}
scanf("%d",&people_num);
for(i = 0;i<people_num;i++){ //记录到场的人
scanf("%s",temp1);
temp1_num =atoi(temp1);
people[temp1_num] = 1;
}
for(i = 0,solo_num = 0;i<100000;i++){
if(people[i] && couple[i] == 100001 || people[i]&&!people[couple[i]]) //人到了但没对象的或者人到了对象没到的都是单身狗
solo_num++;
}
printf("%d\n",solo_num);
for(i = 0,temp_solo_num = 0;i<100000;i++){
if(people[i] && couple[i] == 100001 || people[i]&&!people[couple[i]]){
if(temp_solo_num <solo_num -1)
printf("%05d ",i);
else
printf("%05d",i);
temp_solo_num++;
}
}
return 0;
}