Description
实验室有犬初长成,名曰dogdog,静若处子,动若脱兔,回眸一笑百媚生,工大粉黛无颜色。日前,沉溺于LinkList,无法自拔。 某日,某丹发现某狗在写链表的非降序合并...所以来和狗狗比赛下谁的链表更厉害吧!~ Just ac it!
Input
输入一个数字n,接下来是n组数据,对于每组数据输入一个a、一个b,然后是2条非降序链表。(a表示第一条链表的长度,b表示第二条链表的长度...)
Output
这两条链表合并后的一条非降序链表,并在每组数据之间有行空行。
Sample Input
2 5 4 1 2 3 4 5 6 7 8 9 5 5 1 3 5 7 9 2 4 6 8 10
Sample Output
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10
HINT
赢了就让狗当你基友哦~~ >.<
View Code
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4 typedef struct node
5 {
6 int data;
7 struct node *next;
8 }*pao;
9
10 void merge(pao &h1, pao h2)
11 {
12 pao p1, p2, p3, p4;
13 p1 = h1->next;
14 p2 = h2->next;
15 p3 = h1;
16 while( p1 && p2 )
17 {
18 if( p1->data <= p2->data )
19 {
20 p3 = p1;
21 p1 = p1->next;
22 }
23 else
24 {
25 p4 = p2->next;
26 p2 -> next = p1;
27 p3 -> next = p2;
28 p3 = p2;
29 p2 = p4;
30 }
31 }
32 if( p1 )
33 p3->next = p1;
34 else
35 p3->next = p2;
36 free( h2 );
37 }
38 void look( pao h )
39 {
40 pao p;
41 p = h->next;
42 while(p)
43 {
44 if(p->next == NULL)
45 {
46 printf("%d",p->data);
47 p = p->next;
48 }
49 else
50 {
51 printf("%d ",p->data);
52 p = p->next;
53 }
54 }
55 puts( "" );
56 }
57 void insert(pao &h,int e)
58 {
59 pao p1, p2, q;
60 p1 = ( pao )malloc( sizeof(pao) );
61 p1->data = e;
62 if( h->next )
63 {
64 q = h->next;
65 p2 = h;
66 while( q->next && q->data < p1->data)
67 {
68 p2 = q;
69 q = q->next;
70 }
71 if( q->data >= p1->data )
72 {
73 p1->next = q;
74 p2->next = p1;
75 }
76 else
77 {
78 q->next = p1;
79 p1->next = NULL;
80 }
81 }
82 else
83 {
84 h -> next = p1;
85 p1 -> next = NULL;
86 }
87 }
88 void creat(pao &h,int m)
89 {
90 int i,k;
91 h = ( pao )malloc( sizeof(pao) );
92 h -> next = NULL;
93 for( i = 0; i < m; i++ )
94 {
95 scanf( "%d", &k );
96 insert( h, k );
97 }
98 }
99
100 int main()
101 {
102 int n, a, b;
103 pao h1 = NULL, h2 = NULL;
104 scanf( "%d",&n );
105 while( n-- )
106 {
107
108 scanf( "%d%d",&a, &b );
109 creat( h1, a );
110 creat( h2, b );
111 merge( h1, h2 );
112 look( h1 );
113 if(n != 0)
114 puts( "" );
115 }
116 return 0;
117 }
View Code
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 int cmp( const void *a, const void *b )
5 {
6 //return *(int *)b-*(int *)a; //从大到小~
7 return *(int *)a-*(int *)b;//从小到大
8 }//必要的语句。
9 int main()
10 {
11 int n;
12 scanf( "%d", &n );
13 while( n-- )
14 {
15 int a, b;
16 scanf( "%d%d", &a, &b );
17 int num[10000];
18 int i;
19 for( i = 0; i < a; i++ )
20 scanf( "%d", &num[i] );
21 for( i = a; i < a+b; i++ )
22 scanf( "%d", &num[i] );
23 qsort( num, a+b, sizeof(num[1]),cmp );
24 for( i = 0; i < a+b; i++ )
25 printf( i == a+b-1?"%d\n":"%d ", num[i] );
26 puts( "" );
27 }
28 // system( "pause" );
29 }