• 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. }

     

  • 相关阅读:
    DC电源模块的数字电源优势
    R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化分组散点图(scatter plot)、设置add参数为每个分组添加回归线
    【Linux进程间通信】二、pipe管道
    【22年】2022最新阿里Java面经,转疯了
    Python潮流周刊#1:如何系统地自学Python?
    由小见大!不规则造型按钮解决方案
    SpringApplication启动详解说明
    Redis常用数据类型及其应用场景
    基于Bert模型的中文语义相似度匹配算法(离线模式)
    Self-attention与multi-head self-attention
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/125468119