标签:des style blog io ar os 使用 sp for
6 5 1 23 26 45 66 99 14 21 28 50 100
1 14 21 23 26 28 45 50 66 99 100
#include <iostream> #include <string> #include <stdio.h> #include <string.h> #include <queue> #define INF 99999999 using namespace std; struct node { int data; struct node *next; }; struct node *creat(int n) { int i; node *head, *p, *tail; head = new node; head->next = NULL; tail=head; for(i=0; i<n; i++) { p=new node; cin>>p->data; p->next=NULL; tail->next=p; tail=p; } return head; } struct node *merge(node *h1, node *h2) { struct node *p1, *p2, *tail; p1=h1->next; p2=h2->next; delete h2; //释放h2 tail=h1; while(p1 && p2) { if(p1->data < p2->data)//链上data值小的 { tail->next=p1; tail=p1; p1=p1->next; } else { tail->next=p2; tail=p2; p2=p2->next; } } if(p1) { tail->next=p1; } else { tail->next=p2; } return h1; } int main() { int n, m; cin>>n>>m; int i; node *h1, *h2, *h3, *w; h1=creat(n); h2=creat(m); h3=merge(h1, h2); w=h3->next; for(i=0; i<n+m; i++) { if(i==0) { cout<<w->data; } else { cout<<" "<<w->data; } w=w->next; } cout<<endl; return 0; }
数组实现:
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int a[100]; int b[100]; int c[200], e=0; int main() { int i, j; int n, m; scanf("%d %d", &n, &m ); for(i=0; i<n; i++) { scanf("%d", &a[i] ); } for(j=0; j<m; j++) { scanf("%d", &b[j] ); } i = 0; j = 0; while(i<n && j<m ) { if(a[i] <= b[j] ) { c[e++] = a[i] ; i++; } else { c[e++] = b[j]; j++; } } if(i==n && j<m) { while(j<m) { c[e++] = b[j]; j++; } } else if(j==m && i<n) { while(i<n) { c[e++] = a[i] ; i++; } } for(i=0; i<e; i++) { if(i==0) printf("%d", c[0] ); else printf(" %d", c[i] ); } printf("\n"); return 0; }
标签:des style blog io ar os 使用 sp for
原文地址:http://www.cnblogs.com/yspworld/p/4093989.html