[ACM] median

Time Limit: 3 Sec  Memory Limit: 64 MB
Submit: 729  Solved: 195

Problem Description

A group of data are sequentially arranged in order from small to large, a number in an intermediate position called the median.

1,510,119 example wherein the number of bits is 9. because after sorting, 9 in an intermediate position.

Now to give you some numbers, you find the median.

Entry

A first line of input integer T (1 <= T <= 1000) indicates the number of test data sets. The following line is an odd number M, the set of test data indicates a total of M (1 <= M <= 1000) number. The following line M have mutually different integers, which integers are not greater than 10,000 and not less than -10000.

Export

For each set of test data output an integer number which represents the median M.

Sample input

1
5
1 5 10 11 9

Sample Output

9
#include<iostream>
#include<algorithm>
using namespace std;
int a[1003];
int main(){	
	int n,x;
	cin>>n;
	while(n--){
		cin>>x;
		for(int i=0;i<x;i++)
			cin>>a[i];
		nth_element(a,a+x/2,a+x);
		cout<<a[x/2]<<endl;
	}
} 

 

Published 46 original articles · won praise 39 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_42128813/article/details/103591793