题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028

题目描述:

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

Input

Each input file contains one test case. For each case, the first line contains two integers N (<=100000) 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

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

分析:

输入学生信息,按照指定项进行排序。当按照name或grade排序时如果存在一样的情况就按照学号递增的形式输出。

原本是想用c++做,num和name用string类型表示。但是用c++中的cin进行输入时会有一组数据在最终提交时超时。所以改用scanf输入。

由于scanf不能输入string类型,所以将string类型用char[]代替。

但是如果num和name都用char[]类型,在输出时又会出错,见“错误代码一”。

后面就将num为int型,为了满足num为6位的要求,要用printf("%06d",num) 这种形式输出。

错误代码一:

  1. #include<iostream>
  2. #include<string>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<cstdio>
  7. using namespace std;
  8.  
  9. typedef struct student
  10. {
  11.     char num[6];
  12.     char name[20];
  13.     int grade;
  14. }Student;
  15.  
  16. int n;
  17. bool comparison(Student a,Student b)
  18. {
  19.     if(n==1)
  20.     {
  21.         return strcmp(a.num,b.num)<0;
  22.     }
  23.     else if(n==2)
  24.     {        
  25.         if(strcmp(a.name,b.name) == 0)
  26.             return strcmp(a.num,b.num);
  27.         return strcmp(a.name,b.name)<0;
  28.     }
  29.     else if(n==3)
  30.     {
  31.         if(a.grade == b.grade)
  32.             return strcmp(a.num,b.num) < 0;    
  33.         return a.grade < b.grade;
  34.     }        
  35. }
  36.  
  37. int main()
  38. {
  39.     int M;
  40.     cin>>M>>n;
  41.     vector<Student> s(M);
  42.     int i;
  43.     for(i=0; i<M; i++)
  44.         //cin>>s[i].num>>s[i].name>>s[i].grade;
  45.         scanf("%s%s%d",&s[i].num,&s[i].name,&s[i].grade);
  46.  
  47.     sort(s.begin(),s.end(),comparison);
  48.     for(i=0; i<M; i++)
  49.         cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].grade<<endl;
  50.  
  51.     return 0;
  52. }

最终的输出不正确。但是
原因是???


正确代码:

参考:http://blog.csdn.net/sunbaigui/article/details/8657115

  1. #include<iostream>
  2. #include<string>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<vector>
  6. #include<cstdio>
  7. using namespace std;
  8.  
  9. typedef struct student
  10. {
  11. int num;
  12. char name[20];
  13. int grade;
  14. }Student;
  15.  
  16. int n;
  17. bool comparison(Student a,Student b)
  18. {
  19. if(n==1)
  20. {
  21. return a.num<b.num;
  22. }
  23. else if(n==2)
  24. {
  25. if(strcmp(a.name,b.name) == 0)
  26. return a.num<b.num;
  27. return strcmp(a.name,b.name)<0;
  28. }
  29. else if(n==3)
  30. {
  31. if(a.grade == b.grade)
  32. return a.num<b.num;
  33. return a.grade < b.grade;
  34. }
  35. }
  36.  
  37. int main()
  38. {
  39. int M;
  40. cin>>M>>n;
  41. vector<Student> s(M);
  42. int i;
  43. for(i=0; i<M; i++)
  44. scanf("%d%s%d",&s[i].num,&s[i].name,&s[i].grade);
  45.  
  46. sort(s.begin(),s.end(),comparison);
  47. for(i=0; i<M; i++)
  48. printf("%06d %s %d\n",s[i].num,s[i].name,s[i].grade);
  49.  
  50. return 0;
  51. }

【PAT】1028. List Sorting (25)的更多相关文章

  1. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  2. PAT 甲级 1028. List Sorting (25) 【结构体排序】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1028 思路 就按照 它的三种方式 设计 comp 函数 然后快排就好了 但是 如果用 c++ ...

  3. 【PAT甲级】1028 List Sorting (25 分)

    题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...

  4. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  5. 【PAT】1015 德才论 (25)(25 分)

    1015 德才论 (25)(25 分) 宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得 ...

  6. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. 【PAT】B1055 集体照(25 分)

    很简单的two points问题 ##注意:K是行数 #include<stdio.h> #include<string.h> #include<map> #inc ...

  8. 【PAT】1051 Pop Sequence (25)(25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  9. 【PAT】1063. Set Similarity (25) 待改进

    Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the ...

随机推荐

  1. Boolean Expressions POJ - 2106 (表达式求值)

    The objective of the program you are going to produce is to evaluate boolean expressions as the one ...

  2. 1.4(JavaScript学习笔记) window对象的属性及方法

    一.window对象 window对象代表当前窗口,所有全局对象都是windows的属性, 例如document是window的属性,window.document.writer("&quo ...

  3. Unity ScriptObject创建Asset文件

    创建ScriptObject可以创建带序列化的资源,只保存数据不用绑定在游戏对象上.创建出来的本子资源可以通过资源加载到游戏里使用.这里介绍一下使用Resources加载. 创建好的asset文件也可 ...

  4. Cocoapods报错Unable to satisfy the following requirements

    很多时候我们都会去gitHub上down别人的源码去研究,如果别人的项目用pod集成了,当我们下载好后不外乎cd到项目根目录pod install一下,集成项目所需的库类.今天在我pod instal ...

  5. 《C# to IL》第二章 IL基础

    如果你真的想要理解C#代码,那么最好的方法就是通过理解由C#编译器生成的代码.本章和下面两章将关注于此. 我们将用一个短小的C#程序来揭开IL的神秘面纱,并解释由编译器生成的IL代码.这样,我们就可以 ...

  6. std::string compare

    #include <iostream> #define NULL 0 #define MAX_LIMIT 5 //#define MAX_LENGTH 2 bool ComparePC2S ...

  7. 【微信小程序】view顶部固定或底部固定 + scroll-view中的元素view也可以使用position:fixed;固定选中元素位置

    1.顶端固定核心代码如下: <view class="page__hd" style="position:fixed; top:0;width: 750rpx;&q ...

  8. 【redis】spring boot中 使用redis hash 操作 --- 之 使用redis实现库存的并发有序操作

    示例: @Autowired StringRedisTemplate redisTemplate; @Override public void dealRedis(Dealer dealer) { d ...

  9. javascript无缝滚动原理

    相比之下,无缝拼接能避免切换时出现空白,使用户体验更好! 无缝滚动原理: 制作一个双胞胎,内容跟主体内容一致,样式一致,如果横向排列则并排,当切换的时候,就可以弥补主体空白的地方,其他按普通循环操作即 ...

  10. 为11gR2 Grid Infrastructure增加新的public网络

    在某些环境下,运行11.2版本的RAC数据库的服务器上,连接了多个public网络,那么就会有如下的需求: 给其他的,或者说是新的public网络增加新的VIP地址. 在新的public网络上增加SC ...