fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. typedef struct {
  8. int num; //입력 순서 저장
  9. int age;
  10. string name;
  11. }Person;
  12.  
  13. int cmp(const Person& a, const Person& b)
  14. {
  15. if (a.age == b.age) return a.num < b.num; //나이가 같다면 입력 순으로 올림차순
  16. else return a.age < b.age; //나이순으로 올림차순
  17. }
  18. int main(void)
  19. {
  20. ios::sync_with_stdio(false);
  21. cin.tie(NULL);
  22.  
  23. int n;
  24. cin >> n;
  25.  
  26. Person* arr = new Person[n];
  27.  
  28. for (int i = 0; i < n; i++)
  29. {
  30. cin >> arr[i].age >> arr[i].name;
  31. arr[i].num = i;
  32. }
  33.  
  34. sort(arr, arr + n, cmp);
  35.  
  36. for (int i = 0; i < n; i++)
  37. cout << arr[i].age << " " << arr[i].name << '\n';
  38.  
  39. delete[] arr;
  40. return 0;
  41. }
Success #stdin #stdout 0s 4944KB
stdin
3
21 Junkyu
21 Dohyun
20 Sunyoung
stdout
20 Sunyoung
21 Junkyu
21 Dohyun