section3-6.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Question expression is ambiguous
  2. // reference: http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=20392&messageid=1&deep=0
  3. #include<iostream>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7. struct Student {
  8. int id;
  9. int score;
  10. };
  11. bool compare(Student a, Student b) {
  12. return a.score > b.score;
  13. }
  14. int getRank(int targetScore, vector<Student> students) {
  15. sort(students.begin(), students.end(), compare);
  16. int size = students.size();
  17. int rankNum = 1;
  18. for (int i = 0; i < size; i++) {
  19. if (students.at(i).score > targetScore){
  20. rankNum++;
  21. }
  22. else {
  23. return rankNum;
  24. }
  25. }
  26. return 0;
  27. }
  28. int main()
  29. {
  30. int targetId = 0, targetScore = 0;
  31. vector<Student> students;
  32. int id = 0, score = 0;
  33. while (cin >> targetId) {
  34. while (cin >> id >> score)
  35. {
  36. if (targetId == id)
  37. {
  38. targetScore = score;
  39. }
  40. if (id == 0 && score == 0) {
  41. cout << getRank(targetScore, students) << endl;
  42. students.clear();
  43. break;
  44. }
  45. students.push_back(Student{ id, score });
  46. }
  47. }
  48. return 0;
  49. }