1028 List Sorting (25)(25 分)

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

题目大意:excel表格可以根据任一行排序,模拟excel的排序。

#include <stdio.h>
#include<iostream>
#include <algorithm>
using namespace std;
struct Stu{
string id,name;
int score;
}stu[];
int m;
bool cmp(Stu& a,Stu& b){
if(m==)
return a.id<b.id;
else if(m==){
if(a.name<b.name)
return true;
else if(a.name==b.name)
return a.id<b.id;
}
else if(m==){
if(a.score<b.score)
return true;
else if(a.score==b.score)
return a.id<b.id;
}
return false;
}
int main() {
int n;
cin>>n>>m;
for(int i=;i<n;i++){
cin>>stu[i].id>>stu[i].name;
cin>>stu[i].score;
}
sort(stu,stu+n,cmp);
for(int i=;i<n;i++)
cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].score<<"\n";
return ;
}

//这样写的代码,可以在牛客网上通过,但是在pat上有最后一个测试点运行超时。应该是因为使用string比较,超时了,应该把它们作为int 来比较,所以说c++ 中带的string比较复杂度较高。

#include <stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
using namespace std;
struct Stu{
char name[];
int id,score;
}stu[];
int m;
bool cmp(Stu& a,Stu& b){
if(m==)
return a.id<b.id;
else if(m==){
if(strcmp(a.name,b.name)==)
return a.id<b.id;
else
return strcmp(a.name,b.name)<=;
}
else if(m==){
if(a.score<b.score)
return true;
else if(a.score==b.score)
return a.id<b.id;
}
return false;
}
int main() {
int n;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d%s%d",&stu[i].id,&stu[i].name,&stu[i].score);
}
sort(stu,stu+n,cmp);
for(int i=;i<n;i++)
printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].score);
return ;
}

代码改成了这样,但是一开始我根据题意,name不超过8个字符,我就定义了字符数组是name[8],只通过了3个点,将其改为10,就通过了。懂了!因为如果正好有8个字符,但是不能放最后一个结束符,这就出现了问题,不符合字符数组的定义了!

1.字符数组

题中之所以改为10,是因为使用的字符串%s输入,比如可以直接用char c[ ]="C program";来初始化字符数组,但此时c的长度是10不是9,因为字符串常量的最后由系统加上一个'\0',所以字符数组c中不得不存储了一个'\0'表示结束。

2.strcmp函数用法

当s1>s2时,返回为正数;

当s1=s2时,返回值为0;

当s1<s2时,返回为负数;

返回的号与字符数组的方向是一致的,不一定是1或-1的(C中并没有规定)。

比较的是ASCII字符的大小,

#include <stdio.h>
#include<iostream>
#include<string.h>
using namespace std; int main() {
char a[]={'a','a'};
char b[]={'A','A'};//由于在ASCII中,A的号是65,而a是97
cout<<strcmp(a,b);//所以a是大于b的。
//应该输出一个正数才对。
return ;
}//输出结果为1.

PAT 1028 List Sorting[排序][一般]的更多相关文章

  1. PAT 1028 List Sorting (25分) 用char[],不要用string

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

  2. PAT 1028. List Sorting

    #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> ...

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

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

  4. PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)

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

  5. PAT甲题题解-1028. List Sorting (25)-水排序

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

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

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

  7. 【PAT】1028. List Sorting (25)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028 题目描述: Excel can sort records according to an ...

  8. PAT A1028 List Sorting (25 分)——排序,字符串输出用printf

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

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

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

随机推荐

  1. oracle数据库用户加锁和解锁

    oracle数据库安装好之后,scott之类的用户默认情况下是被锁住的,无法使用scott用户登录数据库.使用有alter user数据库权限的用户登陆,角色选sysdba,执行以下命令: 解锁命令: ...

  2. MAC ox下配置mysql

    下载MySQL安装包:http://dev.mysql.com/downloads/mysql/ 如图,选择dmg包,下载完成,双击后打开对应的pkg包,一直点击下一步,直到安装成功 启动和停止MyS ...

  3. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验九:PS/2模块③ — 键盘与多组合键

    实验九:PS/2模块③ — 键盘与多组合键 笔者曾经说过,通码除了单字节以外,也有双字节通码,而且双字节通码都是 8’hE0开头,别名又是 E0按键.常见的的E0按键有,<↑>,<↓ ...

  4. VC 测试一段程序的运行时间 精确到ms

    分三个步骤 1:声明变量 LARGE_INTEGER litmp; _int64 QPart1,QPart2; double dfMinus,dfFreq, dfTim; QueryPerforman ...

  5. python unittest框架中doCleanups妙用

    偶看unittest官方文档中,发现一个很好用的功能函数doCleanups,看看官方是怎么解释的: doCleanups() This method is called unconditionall ...

  6. linux下jmeter持续集成Jenkins部署时问题解决

    之前成linux下安装了Jenkins,并做了一些简单的工作,这次正好将jmeter也集成进去,在实际操作时发现好多坑,写下做记录 怎么安装这里就不介绍了,网上很多资料,这里只记录问题,以供大家参数 ...

  7. [转]Android中attr自定义标签详解

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:wen= ...

  8. TFS二次开发02——连接TFS

    在上一篇<TFS二次开发01——TeamProjectsPicher>介绍了  TeamProjectsPicher 对象,使用该对象可以很简单的实现连接TFS. 但是如果我们要实现自定义 ...

  9. 《into100-创客+沙龙第4期:互联网产品用户需求挖掘与转化》圆满成功

    5月16日,由麦思博和阿里云联合主办的<into100-创客+沙龙第4期:互联网产品用户需求挖掘与转化>圆满结束.现场100多位来自互联网及软件公司的产品经理.产品总监.开发经理.工程师及 ...

  10. 使用Pangolon在同一副图中,画出两个轨迹,比较误差

    使用 code/ground-truth.txt 和 code/estimate.txt 两条轨迹.请你根据上面公式,实现 RMSE的计算代码,给出最后的 RMSE 结果.作为验算,参考答案为:2.2 ...