ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PAT A1028 List Sorting 列表排序【简单排序 】

2019-01-15 18:05:59  阅读:303  来源: 互联网

标签:Sorting PAT name int according records sorted 排序 ID


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

Excel可以根据任一列对记录进行排序,现在你需要来模仿这个功能

Input Specification:

Each input file contains one test case. For each case, the first line contains two integers N (≤10​^5​​) 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).

第一行包含两个整数N(<10^5)和C,N是记录的总数量,C是记录的列数。之后有N行,每一行包含了一个学生记录。一个学生记录包括他第一无二的ID(一个6位数字) 名字(不超过8个字符的字符串,无空格) 和分数(在0-100之间的整数,包括0和100)

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.

输出N行的排序结果。如果C=1,那么记录需要根据ID号以递增顺序排列。如果C=2,那么记录需要根据姓名按非递减顺序排列。如果C=3,那么记录要根据分数非递减顺序排列。如果有几个学生有相同的姓名或者分数,按照ID号递增顺序排列

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100010;

struct Student{
	int id;
	char name[10];
	int grade;
}stu[maxn];
int C;
bool cmp(Student a,Student b){
    if(C==2&&strcmp(a.name,b.name)!=0) return strcmp(a.name,b.name)<0;
    else if(C==3&&a.grade!=b.grade) return a.grade<b.grade;
    else  return a.id<b.id;
}
int main(){
	int n;
	scanf("%d%d",&n,&C);
	for(int i=0;i<n;i++){
		scanf("%d %s %d",&stu[i].id,stu[i].name,&stu[i].grade);
	}
	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].grade);
	}
	return 0;
}

出错的地方:打印学生id时,注意%06d 不然结果会错误

标签:Sorting,PAT,name,int,according,records,sorted,排序,ID
来源: https://blog.csdn.net/qq_38179583/article/details/86496548

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有