可能做了假题,这道题明显该merge,对两个有序序列进行合并,结果sort一下居然过了,也贴一下merge
sort
#include<iostream>
#include<vector>
#include<algorithm>
#pragma warning (disable:4996)
using namespace std;
vector<long int> a;
int N;
int main()
{
cin >> N;
for (int t = 0;t < N;t++)
{
long int temp;
scanf("%ld", &temp);
a.push_back(temp);
}
cin >> N;
for (int t = 0;t < N;t++)
{
long int temp;
scanf("%ld", &temp);
a.push_back(temp);
}
sort(a.begin(), a.end());
cout << a[(a.size() - 1) / 2] << endl;
}
merge
#include<iostream>
#pragma warning (disable:4996)
#include<stdlib.h>
int main()
{
int N, M;
scanf("%d", &N);
int *s = (int *)malloc(sizeof(int)*N);
for (int t = 0;t < N;t++)
scanf("%d", s + t);
scanf("%d", &M);
s = (int *)realloc(s,sizeof(int)*(M + N));
for (int t = N;t < M + N;t++)
scanf("%d", s + t);
int i = 0, j = N;
int *temp= (int *)malloc(sizeof(int)*(N+M));
int t = 0;
while (i < N&&j < M + N)
{
if (s[i] < s[j])
temp[t++] = s[i++];
else
temp[t++] = s[j++];
}
while (i < N)
temp[t++] = s[i++];
while (j < M + N)
temp[t++] = s[j++];
printf("%d\n", temp[(M + N - 1) / 2]);
}