PTA: Single Dog (25 points) (C language version)

"Single Dog" is the Chinese term of endearment for a single person. This question you find guests from the single large party of thousands of people in order to give special care.

Input format:
input of the first row is given a positive integer N (≤ 50000), is the number of known couple / partner; then N rows, each row is given a couple / partner - for convenience, each corresponding to an ID number, 5 digits, separated by inter-ID (from 00000-99999) with a space; given after a positive integer M (≤10000), the total number of people at the party; then the M line gives guests ID , separated by a space. Topic ensure no bigamy or foot boats.

Output Format:
First, the single output line of a first total number of guests; followed by a second line ID is incremented by the order listed in the single guests. With a space between the partition ID, and last row may not have extra space.

Sample input:
. 3
11111 22222
33333 44444
55555 66666
. 7
55555 444,441,000,088,888 222,221,111,123,333

Output Sample:
. 5
10000 23333 444,445,555,588,888

analysis

If open, respectively a, b respectively store two arrays a couple, after traversing through two for, take up a lot of time does not pass, so here we use an array to store, a [i] = n; in a couple of .
Then we use an array to store the scene the man c, b array to mark id.

#include <stdio.h>
#include <stdlib.h>
#define MAX 100000

int cmpfunc(const void *elemt1, const void *elemt2);

int main()
{
    int n1, n2, i, j, k, n;
    int a[MAX], b[MAX] , c[10001], d[10001];

    for (i = 0; i < MAX; i++)
    {
        a[i] = -1;
        b[i] = -1;
    }
    for (i = 0; i < 10001; i++)
        c[i] = -1;
    scanf("%d", &n1);
    for (i = 0; i < n1; i++)      //牺牲空间换时间。
    {
        int tempa, tempb;
        scanf("%d %d", &tempa, &tempb);
        a[tempa] = tempb;
        a[tempb] = tempa;
    }
    scanf("%d", &n2);
    for (i = 0; i < n2; i++)
    {
        scanf("%d", &c[i]);
        b[c[i]] = 1;
    }
    n = k = 0;             //n是单身狗人数。
    for (i = 0; i < n2; i++)
    {
        int temp;
        temp = a[c[i]];
        if (b[temp] != -1)
            continue;
        else
        {
		d[k++] = c[i];
		n++;
		}
    }
    qsort(d, n, sizeof(int), cmpfunc);
    printf("%d\n", n);
    if (n != 0)                     //r如果没有if判断,当 n = 0时,依然会输出 00000
        printf("%05d", d[0]);       //如果直接%d  则会0 而不是00000,格式错误。
    for (i = 1; i < n; i++)
        printf(" %05d", d[i]);

    return 0;
}
int cmpfunc(const void *elemt1, const void *elemt2)
{
    int *p1 = (int*)elemt1;
    int *p2 = (int*)elemt2;

    return *p1 - *p2;
}
Published 24 original articles · won praise 0 · Views 146

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105151836
Recommended