- #include "iostream"
- #include "stdio.h"
- #include "math.h"
- #include "map"
- #include "vector"
- #include "queue"
- #include "memory.h"
- #include "algorithm"
- #include "string"
- using namespace std;
- #define N 1000005
- #define INF 1<<29
- #define max(a,b) a>b?a:b
- int a[N],b[N];
- int array3[N];
- int MergeArray(int la,int ha,int lb,int hb)
- {
- int i,j,x;
- i=la,j=lb,x=0;
- while(i<=ha&&j<=hb)
- {
- if(a[i]>b[j]) array3[x]=b[j++];
- else array3[x]=a[i++];
- x++;
- }
- while(i<=ha) array3[x++]=a[i++];
- while(j<=hb)array3[x++]=b[j++];
- x--;
- return array3[x/2];
- }
- int main()
- {
- int m,n,i,j;
- while(scanf("%d",&n)!=EOF)
- {
- for(i=0;i<n;i++)
- scanf("%d",&a[i]);
- cin>>m;
- for(j=0;j<m;j++)
- scanf("%d",&b[j]);
- int la,lb,ha,hb,ma,mb;
- la=lb=0;ha=n-1;hb=m-1;
- int res;
- res=MergeArray(la,ha,lb,hb);
- printf("%d\n",res);
- }
- }
解法2:(利用algorithm 中的nth_element())
- #include "iostream"
- #include "stdio.h"
- #include "math.h"
- #include "map"
- #include "vector"
- #include "queue"
- #include "memory.h"
- #include "algorithm"
- #include "string"
- using namespace std;
- #define N 1000005
- #define INF 1<<29
- #define max(a,b) a>b?a:b
- long a[1000000];
- int main()
- {
- long n1,n2,i,mid;
- while(cin>>n1)
- {
- for(i=0;i<n1;i++)
- cin>>a[i];
- cin>>n2;
- for(;i<n1+n2;i++)
- cin>>a[i];
- mid=(i-1)/2;
- nth_element(a,a+mid,a+i);
- cout<<a[mid]<<endl;
- }
- return 0;
- }