fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. struct results {
  6. unsigned int id; //id of the team
  7. unsigned int m; //number of solved problems
  8. };
  9.  
  10. // I guess you're trying to sort on number of solved problems. If not, change `.m` to `.id`
  11. bool comparare(const results lhs, const results rhs) {
  12. return lhs.m > rhs.m;
  13. }
  14.  
  15. int main() {
  16.  
  17. size_t n;
  18.  
  19. std::cout << "Enter number of results: " << std::endl;
  20. std::cin >> n;
  21.  
  22. std::vector<results> standings(n); // Creates std::vector of results with n elements
  23.  
  24. // read in id and number of problems solved
  25. for(size_t i=0; i < n; ++i) {
  26. std::cin >> standings[i].id >> standings[i].m;
  27. }
  28.  
  29. // sort the array
  30. std::sort(standings.begin(), standings.end(), comparare);
  31.  
  32. // output the sorted array's id
  33. for(size_t i = 0; i < standings.size(); ++i) {
  34. std::cout << "In " << i+1 << " place: " << standings[i].id << " with " << standings[i].m << " problems solved." << std::endl;
  35. }
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3468KB
stdin
4 1 2 3 4 5 6 7 8
stdout
Enter number of results: 
In 1 place: 7 with 8 problems solved.
In 2 place: 5 with 6 problems solved.
In 3 place: 3 with 4 problems solved.
In 4 place: 1 with 2 problems solved.