zoukankan      html  css  js  c++  java
  • 编程啦1481—001_02

    001_02

    时间限制:1000 ms  |  内存限制:65535 KB
     
    描述

    合并数列

    给定两个非降序排列的数列A,B。数列中元素的值为int, 元素个数不超过1,000。将两个已排序的数列合并成一个非升序的数列输出。

    [Any Problem: trueshlqsh@gmail.com   ||dengdong1211@sse.buaa.edu.cn||  oeddyo@gmail.com]

    输入

    输入有3m+1行。第一行为测试数据的组数m。下面的3m分别为m组测试数据,每组测试数据的第一行包括a,b两个数,表示接下来两行分别有a个数和b个数,接下来数列A B占两行,每个数列中的元素用空格隔开。

    输出

    输出有m行,对应输入的m组测试数据输出合并后的非升序数列,元素之间用空格隔开。

    样例输入
    2
    5 5
    1 3 5 7 9
    2 4 6 8 10
    2 2
    -10 -1
    -9 -2
    样例输出
    10 9 8 7 6 5 4 3 2 1
    -1 -2 -9 -10
    题目链接:http://www.bianchengla.com/team/123/practise/problem?id=1481

    【code】

     1 #include <stdio.h>
    2 #include <stdlib.h>
    3 struct node
    4 {
    5 int data;
    6 struct node*next;
    7 };
    8 struct node*creat(int n)
    9 {
    10 int i;
    11 struct node*head,*p,*tail;
    12 head=(struct node*)malloc(sizeof(struct node));
    13 head->next=NULL;
    14 tail=head;
    15 for(i=1;i<=n;i++)
    16 {
    17 p=(struct node*)malloc(sizeof(struct node));
    18 scanf("%d",&p->data);
    19 p->next=NULL;
    20 tail->next=p;
    21 tail=p;
    22 }
    23 return head;
    24 }
    25 void list(struct node * l)
    26 {
    27 struct node *r;
    28 r=l;
    29 while(r->next->next!=NULL)
    30 {
    31 printf("%d ",r->next->data);
    32 r=r->next;
    33 }
    34 printf("%d\n",r->next->data);
    35 }
    36 struct node *merge(struct node *head1, struct node *head2)
    37 {
    38 struct node *p1,*p2,*tail;
    39 p1=head1->next;
    40 p2=head2->next;
    41 tail=head1;
    42 free(head2);
    43 while(p1&&p2)
    44 {
    45 if(p1->data<p2->data)
    46 {
    47 tail->next=p1;
    48 tail=p1;
    49 p1=p1->next;
    50 }
    51 else
    52 {
    53 tail->next=p2;
    54 tail=p2;
    55 p2=p2->next;
    56 }
    57 }
    58 if(p1)
    59 tail->next=p1;
    60 else
    61 tail->next=p2;
    62 return (head1);
    63 }
    64 void change(struct node*h)
    65 {
    66 struct node*p=h->next,*q=h->next;
    67 q=q->next;
    68 p->next=NULL;
    69 p=q;
    70 while(p!=NULL)
    71 {
    72 q=q->next;
    73 p->next=h->next;
    74 h->next=p;
    75 p=q;
    76 }
    77 }
    78 int main()
    79 {
    80 int n,m,t;
    81 scanf("%d",&t);
    82 while(t--)
    83 {
    84 struct node *head1,*head2,*p;
    85 scanf("%d%d",&n,&m);
    86 head1=creat(n);
    87 head2=creat(m);
    88 p=merge(head1,head2);
    89 change(p);
    90 list(p);
    91 }
    92 return 0;
    93 }

    myblog:www.bearac.com


     

  • 相关阅读:
    Hadoop错误之namenode宕机的数据恢复
    Hadoop双namenode配置搭建(HA)
    Hadoop2之NameNode HA详解
    基于zookeeper的高可用Hadoop HA集群安装
    Spring Boot集成JPA的Column注解命名字段无效的问题
    Spring Boot使用Druid连接池基本配置
    Java设计模式六大原则之场景应用分析
    下半部和推后运行的工作
    SecureCRT连接linux,vim颜色显示问题
    Objective-C MacOS以管理员权限执行程序
  • 原文地址:https://www.cnblogs.com/pony1993/p/2374086.html
Copyright © 2011-2022 走看看