标签:
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; class Seqlist{ public: int last; int sz; int *data; Seqlist(int _sz=0){ ///构造函数 data = new int[_sz]; last = -1; sz = _sz; } void Insert(int i, int value){ ///插入 *(data+i) = value; last = max(last, i); } void Output(){ ///输出 int i; for(i=0; i<=this->last; ++i){ cout<< (*(data+i))<< endl; } } void Sort(){ ///排序 for(int i=0; i<=last; ++i){ int k=i; for(int j=i+1; j<=last; ++j){ if(*(data+k) > *(data+j)){ int temp = j; j=k; k=temp; } } int temp = *(data+k); *(data+k) = *(data+i); *(data+i) = temp; } } }; int main(){ int n,m,sum,cnt=0,x; cin>> n; Seqlist seq1(n); for(int i=0; i<n; ++i){ cin>> x; seq1.Insert(i, x); } cin>> m; Seqlist seq2(m); for(int i=0; i<m; ++i){ cin>> x; seq2.Insert(i, x); } sum = n + m; Seqlist seq(sum); int i=0,j=0; seq1.Sort(); seq2.Sort(); while(i<n && j<m){ if(*(seq1.data+i) == *(seq2.data+j)){ seq.Insert(cnt, *(seq1.data+i)); ++i; ++j; } else if(*(seq1.data+i) < *(seq2.data+j)){ seq.Insert(cnt, *(seq1.data+i)); ++i; } else{ seq.Insert(cnt, *(seq2.data+j)); ++j; } ++cnt; } while(i<n){ seq.Insert(cnt, *(seq1.data+i)); ++i; ++cnt; } while(j<m){ seq.Insert(cnt, *(seq2.data+j)); ++j; ++cnt; } seq.Output(); return 0; }
标签:
原文地址:http://www.cnblogs.com/Kurokey/p/5887160.html