# [PAT解题报告] Median

```#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

long long a[1000006], b[1000006];

int main() {
int na,nb;
scanf("%d",&na);
for (int i = 0; i < na; ++i) {
scanf("%lld",a + i);
}
scanf("%d", &nb);
for (int j = 0; j < nb; ++j) {
scanf("%lld", b + j);
}
for (int i = 0, j = 0, k = 0, c = 0,want = (na + nb - 1) >> 1; (i < na) || (j < nb); ++k) {
if (i >= na) {
c = 1;
}
else if (j >= nb) {
c = 0;
}
else {
c = (a[i] < b[j])?0:1;
}
if (c == 0) {
if (k == want) {
printf("%lld\n",a[i]);
break;
}
++i;
}
else {
if (k == want) {
printf("%lld\n",b[j]);
break;
}
++j;
}
}
return 0;
}
```

#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn =2000010; LL numa[maxn]; int main() {     int lena,lenb;     ios::sync_with_stdio(false);     cin>>lena;     for(int i=1;i<=lena;i++) cin>>numa[i];     cin>>lenb;     for(int i=lena+1;i<=lenb+lena;i++) cin>>numa[i];     sort(numa+1,numa+lena+lenb+1);     cout<<numa[(lena+lenb+1)/2]<<endl; }

09-29 01:19