On Mr. Z's ranch, being a "Brilliant Cow" requires not only weighing in but also passing an intelligence test. The annual intelligence test is extremely rigorous, with only about half of the cows able to pass. Due to the large number of cows participating in the test, determining the "passing line" for the intelligence test has become a headache for Mr. Z. Therefore, he has asked you to solve this problem.
Each cow participating in the test will receive a score (an integer from 0 to 100). After arranging all the scores in descending order, the score in the middle position will be provided to Mr. Z as the "reference score" to help him decide the "passing line." If the number of cows taking the test is even, there will be two middle scores; in this case, the "reference score" will be the smaller of the two.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The first line of input is an integer T (1 ≤ T ≤ 100), representing the total number of test cases. The first line of each test case is an integer N (1 ≤ N ≤ 40000), representing the number of cows taking the test. The following N lines provide the test scores of the N cows, with each line containing a positive integer between 0 and 100. | For each test case, output one answer, the "reference score." |
1 6 97 45 78 62 81 79 | 78 |
Thought Process
Use sorting to arrange the scores. If N is even, output the data at the N/2-1 position in the array. If N is odd, output the data at the N/2 position in the array.
Sample Code-ZeroJudge D153: IQ Test
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for (int i = 0; i<T; i++) {
int N, cow[50000] = {};
cin >> N;
for (int j = 0; j<N; j++) {
cin >> cow[j];
}
sort(cow, cow+N);
if (N % 2 == 0) cout << cow[N/2-1] << "\n";
else cout << cow[N/2] << "\n";
}
}
//ZeroJudge D153
//Dr. SeanXD