PAT 甲级 1028 List Sorting (25 分) STL +sort

灰太狼 2024-02-19 21:29 156阅读 0赞

1028 List Sorting (25 分)

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 90

Sample Output 3:

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

注意排序规则

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7. struct node{
  8. string id,name;
  9. int grade;
  10. };
  11. int cmp1(node a,node b)
  12. {
  13. return a.id<b.id;
  14. }
  15. int cmp2(node a,node b)
  16. {
  17. if(a.name==b.name)
  18. return a.id<b.id;
  19. else
  20. return a.name<b.name;
  21. }
  22. int cmp3(node a,node b)
  23. {
  24. if(a.grade==b.grade)
  25. return a.id<b.id;
  26. else
  27. return a.grade<b.grade;
  28. }
  29. int main()
  30. {
  31. int n,c,i,j;
  32. node t;
  33. vector<node> s;
  34. cin>>n>>c;
  35. for(i=0;i<n;i++)
  36. {
  37. cin>>t.id>>t.name>>t.grade;
  38. s.push_back(t);
  39. }
  40. switch(c)
  41. {
  42. case 1:sort(s.begin(),s.end(),cmp1);break;
  43. case 2:sort(s.begin(),s.end(),cmp2);break;
  44. case 3:sort(s.begin(),s.end(),cmp3);break;
  45. default: break;
  46. }
  47. for(i=0;i<s.size();i++)
  48. cout<<s[i].id<<" "<<s[i].name<<" "<<s[i].grade<<endl;
  49. return 0;
  50. }

发表评论

表情:
评论列表 (有 0 条评论,156人围观)

还没有评论,来说两句吧...

相关阅读