题目链接: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

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1

000001 Zoe 60
000007 James 85
000010 Amy 90

Sample Input 2

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

Sample Output 2

000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60

Sample Input 3

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

Sample Output 3

000001 Zoe 60
000007 James 85
000002 James 90
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) 这种形式输出。

错误代码一:

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std; typedef struct student
{
    char num[6];
    char name[20];
    int grade;
}Student; int n;
bool comparison(Student a,Student b)
{
    if(n==1)
    {
        return strcmp(a.num,b.num)<0;
    }
    else if(n==2)
    {        
        if(strcmp(a.name,b.name) == 0)
            return strcmp(a.num,b.num);
        return strcmp(a.name,b.name)<0;
    }
    else if(n==3)
    {
        if(a.grade == b.grade)
            return strcmp(a.num,b.num) < 0;    
        return a.grade < b.grade;
    }        
} int main()
{
    int M;
    cin>>M>>n;
    vector<Student> s(M);
    int i;
    for(i=0; i<M; i++)
        //cin>>s[i].num>>s[i].name>>s[i].grade;
        scanf("%s%s%d",&s[i].num,&s[i].name,&s[i].grade);     sort(s.begin(),s.end(),comparison);
    for(i=0; i<M; i++)
        cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].grade<<endl;     return 0;
}

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


正确代码:

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

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std; typedef struct student
{
int num;
char name[20];
int grade;
}Student; int n;
bool comparison(Student a,Student b)
{
if(n==1)
{
return a.num<b.num;
}
else if(n==2)
{
if(strcmp(a.name,b.name) == 0)
return a.num<b.num;
return strcmp(a.name,b.name)<0;
}
else if(n==3)
{
if(a.grade == b.grade)
return a.num<b.num;
return a.grade < b.grade;
}
} int main()
{
int M;
cin>>M>>n;
vector<Student> s(M);
int i;
for(i=0; i<M; i++)
scanf("%d%s%d",&s[i].num,&s[i].name,&s[i].grade); sort(s.begin(),s.end(),comparison);
for(i=0; i<M; i++)
printf("%06d %s %d\n",s[i].num,s[i].name,s[i].grade); return 0;
}

【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. pair 对组

    pair 对组 c++ 基础 2016-05-10 19:42 154人阅读 评论(0) 收藏 举报  分类: 头文件的函数精粹(12)  版权声明:本文为博主原创文章,未经博主允许不得转载. 与关联 ...

  2. hibernate 基于主键的单向一对一关联映射

    1.设计表结构 表结构对于基于外键的关联关系来说就少了外键的关联列,并且两张表共用同一个ID,表示一对一. 2.创建Person对象 3.创建IdCard对象 4.写hbm.xml文件 5.生成数据库 ...

  3. 【BZOJ 3133】 3133: [Baltic2013]ballmachine (线段树+倍增)

    3133: [Baltic2013]ballmachine Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 148  Solved: 66 Descri ...

  4. Hash表及hash算法的分析

    Hash表中的一些原理/概念,及根据这些原理/概念: 一.       Hash表概念 二.       Hash构造函数的方法,及适用范围 三.       Hash处理冲突方法,各自特征 四.   ...

  5. 【8.22校内测试】【数学】【并查集】【string】

    今天的t2t3能打出来80分的暴力都好满足啊QwQ.(%%%$idy$ 今天的签到题,做的时候一眼就看出性质叻qwq.大于11的所有数分解合数都可以用4.6.9表示,乱搞搞就可以了. #include ...

  6. SQL Server 事务复制爬坑记

    SQL Server 复制功能折腾了好几天了,现特将其配置过程以及其间遇到的问题记录下来,以备日后查阅.同时,也让“同道”同学们少走不必要的弯路.如果有不对之处,欢迎大家指正,欢迎沟通交流. 一.复制 ...

  7. Codeforces Round #303 (Div. 2) A. Toy Cars 水题

     A. Toy Cars Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...

  8. mysqldump之字符集问题解决

    最近导数据的需求真多,年底了大家都要做分析吗?近期使用mysqldump导出的中文出现乱码问题. mysqldump -uroot -ppassword -S /tmp/mysql9991.sock ...

  9. 模板引擎之hogan.js

    hogan.js 语法简单,且支持循环数据: 基本语法: 1. 标签可以嵌套使用 2. {{data}} 转义的变量,不会渲染html标签 3. {{{data}}} 不转义的变量,会渲染html标签 ...

  10. js之iframe父、子页面通信

    注意事项 一 . 页面加载顺序:一般先加载完父页面才会去加载子页面,所以:必须要确保在iframe加载完成后再进行操作,如果iframe还未加载完成就开始调用里面的方法或变量,会产生错误.判断ifra ...