PAT : 数据结构与算法题目集(中文)7-53 两个有序序列的中位数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/belous_zxy/article/details/86998739

深坑!题目中说的并集是两组数简简单单的一起,并不需要去重?!!去重的话最后一个测试点是过不去的。。。2333

采用增序读取生成链表,输出中间值即可。

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *Nodep;
typedef struct Node
{
    int Value;
    Nodep Next;
} Node;
int getdigit(void)
{
    int re = 0;
    int symbol = 1;
    bool fis = true;
    while (1)
    {
        char ch = getchar();
        if (ch == ' ' || ch == '\n' || ch == EOF)
        {
            if (fis)
                continue;
            return re * symbol;
        }
        else if (ch == '-')
            symbol = -1;
        else
        {
            re = re * 10 + ch - '0';
        }
        fis = false;
    }
}

int main(int argc, char **argv)
{
    int cnt = 2 * getdigit();
    int cnd = 0;
    Nodep point = (Nodep)malloc(sizeof(Node));
    point->Next = NULL;
    Nodep process = point;
    for (int i = 1; i <= cnt; i++)
    {
        if (i == cnt / 2 + 1)
            process = point;
        int temp = getdigit();
        while (process->Next != NULL && process->Next->Value < temp)
            process = process->Next;
        Nodep sp = (Nodep)malloc(sizeof(Node));
        sp->Value = temp;
        sp->Next = process->Next;
        process->Next = sp;
        process = sp;
    }
    cnt = (cnt + 1) / 2;
    while (cnt--)
        point = point->Next;
    printf("%d\n", point->Value);
    return EXIT_SUCCESS;
}

END 

猜你喜欢

转载自blog.csdn.net/belous_zxy/article/details/86998739