• 1028 List Sorting


    Excel can sort records according to any column. Now you are supposed to imitate this function.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student's record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

    Output Specification:

    For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID's in increasing order.

    Sample Input 1:

    1. 3 1
    2. 000007 James 85
    3. 000010 Amy 90
    4. 000001 Zoe 60

    Sample Output 1:

    1. 000001 Zoe 60
    2. 000007 James 85
    3. 000010 Amy 90

    Sample Input 2:

    1. 4 2
    2. 000007 James 85
    3. 000010 Amy 90
    4. 000001 Zoe 60
    5. 000002 James 98

    Sample Output 2:

    1. 000010 Amy 90
    2. 000002 James 98
    3. 000007 James 85
    4. 000001 Zoe 60

    Sample Input 3:

    1. 4 3
    2. 000007 James 85
    3. 000010 Amy 90
    4. 000001 Zoe 60
    5. 000002 James 9

    Sample Output 3:

    1. 000002 James 9
    2. 000001 Zoe 60
    3. 000007 James 85
    4. 000010 Amy 90
    1. #include <iostream>
    2. #include <algorithm>
    3. using namespace std;
    4. struct Stu {
    5. string id;
    6. string name;
    7. int grade;
    8. } a[100010];
    9. bool cmp1(Stu s1, Stu s2) {
    10. return s1.id < s2.id;
    11. }
    12. bool cmp2(Stu s1, Stu s2) {
    13. return s1.name == s2.name ? s1.id < s2.id : s1.name < s2.name;
    14. }
    15. bool cmp3(Stu s1, Stu s2) {
    16. return s1.grade == s2.grade ? s1.id < s2.id : s1.grade < s2.grade;
    17. }
    18. int main() {
    19. int n, c;
    20. cin >> n >> c;
    21. for (int i = 0; i < n; i++) {
    22. cin >> a[i].id >> a[i].name >> a[i].grade;
    23. }
    24. if (c == 1) {
    25. sort(a, a + n, cmp1);
    26. } else if (c == 2) {
    27. sort(a, a + n, cmp2);
    28. } else if (c == 3) {
    29. sort(a, a + n, cmp3);
    30. }
    31. for (int i = 0; i < n; i++) {
    32. cout << a[i].id << ' ' << a[i].name << ' ' << a[i].grade << endl;
    33. }
    34. return 0;
    35. }

     

  • 相关阅读:
    盘点JAVA中基于CAS实现的原子类, 你知道哪些?
    不花钱就能完美解决大型离散非标设备生产行业企业信息化
    Linux 性能分析工具- Atop安装和使用
    类和对象收尾
    当你的公司突然开始大量的裁员,被留下的你,真的准备好面对以后了吗?
    浙大MBA二战上岸:笔试备考辛酸路
    位图的使用与实现
    【Go】Go 语言切片(Slice)
    P3799 妖梦拼木棒——枚举+组合数学
    详细讲解前端如何给后端传输数据,后端如何给前端传输数据(java)
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/125468119