中位数,哪位大佬帮我一下
输入1)第一行整数T表示下面有T组测试案例,每个案例占一行;
2)第二行开始的、每行第一个整数N表示这行案例有N个样本值(都为整数),数据之间以空格分隔。
输出
每个测试案例输出占一行,不保留小数。
样例输入
2
5 10 20 60 80 70
8 23 29 20 32 23 21 33 25
样例输出
60
24
哪位会写呀.我是新学编程的萌新.
程序代码:#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
int main( void )
{
unsigned T;
cin >> T;
for( size_t i=0; i!=T; ++i )
{
unsigned N;
cin >> N;
std::vector<int> arr( N );
std::copy_n( std::istream_iterator<int>(cin), N, arr.begin() );
auto middle = std::next(arr.begin(),(N-1)/2);
std::nth_element( arr.begin(), middle, arr.end() );
if( N%2 == 1 )
{
cout << *middle << '\n';
}
else
{
auto middle2 = std::min_element( middle+1, arr.end() );
cout << (*middle + *middle2)/2 << '\n';
}
}
}