fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string>
  6. #include <limits>
  7. #include <iterator>
  8.  
  9. int main()
  10. {
  11. unsigned int numberOfExamples;
  12. if (!(std::cin >> numberOfExamples) || numberOfExamples == 0)
  13. return EXIT_FAILURE;
  14.  
  15. while (numberOfExamples-- > 0)
  16. {
  17. unsigned int n_tests = 0;
  18. if (std::cin >> n_tests)
  19. {
  20. if (n_tests == 0)
  21. continue;
  22.  
  23. // consume remainder of test lines
  24. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  25.  
  26. // read set of values
  27. std::string line;
  28. if (std::getline(std::cin, line))
  29. {
  30. std::istringstream iss(line);
  31. std::vector<int> v {
  32. std::istream_iterator<int>(iss),
  33. std::istream_iterator<int>() };
  34.  
  35. if (v.size() > 0)
  36. {
  37. std::sort(v.begin(), v.end());
  38.  
  39. if (v.size() % 2 == 0)
  40. std::cout << v[v.size()/2-1] << '\n';
  41.  
  42. else
  43. std::cout << v[v.size()/2] << '\n';
  44. }
  45. }
  46. }
  47. else
  48. {
  49. std::cerr << "Failed to read number of test values\n";
  50. }
  51. }
  52. }
  53.  
Success #stdin #stdout 0s 3476KB
stdin
2
5
12 4 22 31 32
8
22 33 44 11 55 66 88 99
stdout
22
44