zoukankan      html  css  js  c++  java
  • 2-13. 平均两个有序序列(25)(ZJU_PAT 名单 | 排列 )

    主题链接:http://pat.zju.edu.cn/contests/ds/2-13


    已知有两个等长的非降序序列S1, S2, 设计函数求S1与S2并集的中位数。有序序列A0, A1…AN-1的中位数指A(N-1)/2的值,即第[(N+1)/2]个数(A0为第1个数)。

    输入格式说明:

    输入分3行。第1行给出序列的公共长度N(0<N<=100000),随后每行输入一个序列的信息。即N个非降序排列的整数。数字用空格间隔。

    输出格式说明:

    在一行中输出两个输入序列的并集序列的中位数。

    例子输入与输出:

    序号 输入 输出
    1
    5
    1 3 5 7 9
    2 3 4 5 6
    
    4
    
    2
    6
    -100 -10 1 1 1 1
    -50 0 2 3 4 5
    
    1
    
    3
    3
    1 2 3
    4 5 6
    
    3
    
    4
    3
    4 5 6
    1 2 3
    
    3
    
    5
    1
    2
    1
    
    1
    



    PS:

    这题说多了都是泪,怪自己智商捉急,看见题目里的并集。就煞笔的去了重,然后……然后最后一个案例WA到吐都过不了!用了各种方法…………


    代码一例如以下(链表)

    #include <cstdio>
    #include <cstring>
    #include <malloc.h>
    #define LEN sizeof(struct node)
    struct node
    {
        int x;
        struct node *next;
    };
    int N;
    struct node *creat()
    {
        struct node *p1, *p2, *head1;
        p1=p2=head1=(struct node*)malloc(LEN);
        int t = N;
        head1 = NULL;
        int n = 0;
        while(t--)
        {
            p1 = (struct node *)malloc(LEN);
            scanf("%d",&p1->x);
            n++;
            if(n == 1)
                head1 = p1;
            else
                p2->next = p1;
            p2 = p1;
    
        }
        p2->next = NULL;
        return head1;
    }
    
    struct node *findd(struct node *p1, struct node *p2)
    {
        struct node *p3;
        p3 = NULL;
        if(p1 == NULL)
            return p2;
        if(p2 == NULL)
            return p1;
        if(p1->x < p2->x)
        {
            p3 = p1;
            p3->next = findd(p1->next,p2);
        }
        else if(p1->x == p2->x)
        {
            p3 = p1;
            p3->next = findd(p1->next,p2);
        }
        else
        {
            p3 = p2;
            p3->next = findd(p1,p2->next);
        }
        return p3;
    }
    void print(struct node *head)
    {
        struct node *p;
        p = head;
        int n = 0;
        while(p!=NULL)
        {
            if(n == (2*N-1)/2)
            {
                printf("%d
    ",p->x);
                break;
            }
            p = p->next;
            n++;
        }
    }
    int main()
    {
        scanf("%d",&N);
        struct node *p, *head1, *head2;
        head1 = creat();
        head2 = creat();
        p = findd(head1, head2);
        print(p);
        return 0;
    }
    

    代码二例如以下(数组):

    #include <cstdio>
    #include <cstring>
    const int maxn = 100017;
    int a[maxn], b[maxn], c[maxn];
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            for(int i = 0; i < n; i++)
            {
                scanf("%d",&a[i]);
            }
            for(int i = 0; i < n; i++)
            {
                scanf("%d",&b[i]);
            }
            int l = 0;
            int i = 0, j = 0;
            while(i < n && j < n)
            {
                if(a[i] == b[j])
                {
                    c[l++] = a[i];
                    c[l++] = b[j];//这一行開始没加WA到吐
                    i++,j++;
                }
                if(a[i] < b[j])
                {
                    c[l++] = a[i];
                    i++;
                }
                else
                {
                    c[l++] = b[j];
                    j++;
                }
            }
            while(i < n)
            {
                c[l++] = a[i];
                i++;
            }
            while(j < n)
            {
                c[l++] = b[j];
                j++;
            }
            printf("%d
    ",c[(l-1)/2]);
        }
        return 0;
    }
    
    
    


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Database Security: Database Inference
    Database Security: Database Vulnerability
    Database Security: Application Access Assessment
    Database Security :Access Control
    【Database Security 】What Students Need to Know
    数据库原理及安全
    实验十 团队作业6:团队项目系统设计改进与详细设计
    实验九 团队作业5—团队项目需求改进与系统设计
    实验八 团队作业4:基于原型的团队项目需求调研与分析
    实验七 团队作业3:团队项目原型设计与开发
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4939980.html
Copyright © 2011-2022 走看看