PAT 1028 List Sorting[排序][一般]
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[100000]; int m; bool cmp(Stu& a,Stu& b){ if(m==1) return a.id<b.id; else if(m==2){ if(a.name<b.name) return true; else if(a.name==b.name) return a.id<b.id; } else if(m==3){ 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=0;i<n;i++){ cin>>stu[i].id>>stu[i].name; cin>>stu[i].score; } sort(stu,stu+n,cmp); for(int i=0;i<n;i++) cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].score<<"\n"; return 0; }
//这样写的代码,可以在牛客网上通过,但是在pat上有最后一个测试点运行超时。应该是因为使用string比较,超时了,应该把它们作为int 来比较,所以说c++ 中带的string比较复杂度较高。
#include <stdio.h> #include<iostream> #include <algorithm> #include<string.h> using namespace std; struct Stu{ char name[10]; int id,score; }stu[100000]; int m; bool cmp(Stu& a,Stu& b){ if(m==1) return a.id<b.id; else if(m==2){ if(strcmp(a.name,b.name)==0) return a.id<b.id; else return strcmp(a.name,b.name)<=0; } else if(m==3){ 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=0;i<n;i++){ scanf("%d%s%d",&stu[i].id,&stu[i].name,&stu[i].score); } sort(stu,stu+n,cmp); for(int i=0;i<n;i++) printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].score); return 0; }
代码改成了这样,但是一开始我根据题意,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[2]={'a','a'}; char b[2]={'A','A'};//由于在ASCII中,A的号是65,而a是97 cout<<strcmp(a,b);//所以a是大于b的。 //应该输出一个正数才对。 return 0; }//输出结果为1.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?