欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT-A1028 List Sorting【排序】

程序员文章站 2022-07-15 11:11:02
...

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

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).

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.

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

 简单的结构体排序,sort自定义排序算法。虽然很简单,但是险过,算法时间和空间都很差。希望二刷可以改善这个问题

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
struct stu{
	string id;
	string name;
	int grade;
}data[100000];
bool sortById(const stu t1,const stu t2){
	return t1.id<t2.id;
}
bool sortBygrade(const stu t1,const stu t2){
	if(t1.grade<t2.grade){
		return true;
	}else if(t1.grade==t2.grade){
		return t1.id<t2.id;
	}
	return false;
}
bool sortByname(const stu t1,const stu t2){
	if(t1.name<t2.name){
		return true;
	}
	else if(t1.name==t2.name){
		return t1.id<t2.id;
	}
	return false;
}
int main(){
	int num,flag;
	string id,name;
	int grade;
	cin>>num>>flag;
	for(int a=0;a<num;a++){
		cin>>id>>name>>grade;
		data[a].id=id;
		data[a].name=name;
		data[a].grade=grade;
	}
	if(flag==1){
		sort(data,data+num,sortById);
	}
	else if(flag==2){
		sort(data,data+num,sortByname);
	}
	else if(flag==3){
		sort(data,data+num,sortBygrade);
	}
	for(int i=0;i<num;i++){
		cout<<data[i].id<<" "<<data[i].name<<" "<<data[i].grade<<endl;
	}
	return 0;
}

PAT-A1028 List Sorting【排序】 一个超时,剩下的虽然过了,但是有这运行时间与空间太大的问题。针对上述代码进行优化

优化思路

  • 轻装上阵。即能少装几层有少装。比如这道题我们一拿到题就知道使用结构体保存每一个学生的数据。面对多条学生纪录,这里有两个办法进行保存,一个是使用结构体数组保存,一个是使用Vector泛型保存。使用vector相当于多套了一层,所以能不使用尽量不使用(PS,第一次代码使用的是vector也是有一次没运行超时,不过没贴代码)
  • 不要脱了裤子放屁。没必要的东西,不要额外折腾。就数据的输出而言,可以直接将数据保存到数组的相应位置,而不是先保存到临时变量然后在赋值。比如36-41行代码
  • 能不用高级变量的尽量不用高级变量。什么意思呢,就拿学生id来说,一共是6位数字,使用基础变量int绝对可以装得下,而我上面使用了string,姓名明确指出只有10位字符,可以使用字符数组完成,我用的string,属于额外的空间开销。
  • 有现成的方法尽量不要自己自信去手写,极其不建议自己造*。比如姓名的比较,即字符串的比较,有现成得笔算算法,strcmp(char [] a,char [] b) ,返回一个int类型数字。等于0表示两个字符串相等,小于零表示第一个字符串小于第二个字符串,大于零表示第一个字符串大于第二个字符串。
  • 追明显的,能用scanf输入的不用cin,能用printf输出的不用cout

  改良版

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
struct Student{
    int id;
    int grade;
    char name[10];
}students[100000];
bool sortById(const Student t1,const Student t2){
	return t1.id<t2.id;
}
bool sortBygrade(const Student t1,const Student t2){
	int cmp = t1.grade - t2.grade;
        if(cmp != 0) return cmp <= 0;
        return t1.id < t2.id;
}
bool sortByname(const Student t1,const Student t2){
	
		int cmp = strcmp(t1.name, t2.name);
        if(cmp != 0) return cmp <= 0;
        return t1.id < t2.id;
}
int main(){
	int n;
	int flag;
    scanf("%d %d", &n, &flag);
    for(int i = 0; i < n; i++){
        scanf("%d %s %d", &students[i].id, students[i].name, &students[i].grade);
    }
	if(flag==1){
		sort(students, students + n, sortById);
	}else if(flag==2){
		sort(students, students + n, sortByname);
	}else if(flag==3){
		sort(students, students + n, sortBygrade);
	}
    
    for(int  i = 0; i < n; i++)
        printf("%06d %s %d\n", students[i].id, students[i].name, students[i].grade);
    return 0;
}

 

PAT-A1028 List Sorting【排序】