C++sort函数使用(成绩排名)+ 1025 PAT Ranking (25分)

需求:使用sort实现先按照成绩降序排名,如果名次相同则按照姓名按照字典序从小到大,实现排名。
分析:第一种方式给出将名次作为结构体的一个属性,第二种方式则直接输出

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct Student{
	char name[10];
	int score;
	int mingci;
}stus[5];
bool cmp(Student a,Student b){
	if(a.score!=b.score) 
		return a.score>b.score;
	else
		return strcmp(a.name,b.name)<0;
}
int main(){
	stus[0].score = 86;
	strcpy(stus[0].name,"li");
	stus[1].score = 83;
	strcpy(stus[1].name,"wei");
	stus[2].score = 83;
	strcpy(stus[2].name,"zhao");
	stus[3].score = 99;
	strcpy(stus[3].name,"zhang");
	sort(stus,stus+4,cmp);
	//记录排名 
	stus[0].mingci = 1 ;
	for(int i=1;i<4;i++){
		if(stus[i].score == stus[i-1].score)
			stus[i].mingci = stus[i-1].mingci;
		else
			stus[i].mingci = i+1;
	} 
	for(int i=0;i<4;i++){
		printf("\n%s %d %d\n",stus[i].name,stus[i].score,stus[i].mingci );	
	}
	return 0;
}

注意使用strcmp函数和strcpy函数
运行结果为

第二种方法,即采用直接输出的方式。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct Student{
	char name[10];
	int score;
}stus[5];
bool cmp(Student a,Student b){
	if(a.score!=b.score) 
		return a.score>b.score;
	else
		return strcmp(a.name,b.name)<0;
}
int main(){
	stus[0].score = 86;
	strcpy(stus[0].name,"li");
	stus[1].score = 83;
	strcpy(stus[1].name,"wei");
	stus[2].score = 83;
	strcpy(stus[2].name,"zhao");
	stus[3].score = 99;
	strcpy(stus[3].name,"zhang");
	sort(stus,stus+4,cmp);
	int r = 1; 
	for(int i=0;i<4;i++){
		if(i > 0 && (stus[i].score!=stus[i-1].score))
		{
				r = i + 1;
				printf("\n%s%d排名%d \n",stus[i].name,stus[i].score,r);
			}
		else	{
				printf("\n%s%d排名%d \n",stus[i].name,stus[i].score,r);}

	}
	return 0;
}

运行结果如下

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
具体实现:

#include<stdio.h>
#include<string.h> 
#include<algorithm>
using namespace std;
struct Student{
	char id[15];//准考证号 
	int score;//分数 
	int location_id;//考场号
	int location_rank;//考场排名 
}stu[30010];
bool cmp(Student a,Student b){
	if(a.score!=b.score)
		return a.score>b.score;
	else
		return strcmp(a.id,b.id)<0; 
}
int main(){
	int n,k,num = 0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&k);
		for(int j = 0;j < k;j++){
			scanf("%s %d",stu[num].id,&stu[num].score);
			stu[num].location_id = i;
			num++;
		}	
	sort(stu+num-k,stu+num,cmp);
	stu[num-k].location_rank = 1;
	for(int j = 1;j<k;j++){
		if(stu[num-k+j].score == stu[num-k+j-1].score)
			stu[num-k+j].location_rank = stu[num-k+j-1].location_rank;
		else
			stu[num-k+j].location_rank = j+1;
	}

	}
	printf("%d\n",num);
	sort(stu,stu+num,cmp);
	int r = 1;
	for(int i = 0;i<num;i++){
		if(i > 0 && stu[i].score != stu[i-1].score)
			r = i + 1;
		printf("%s ",stu[i].id);
		printf("%d %d %d\n",r,stu[i].location_id,stu[i].location_rank);
	}
	return 0;
}
posted @ 2021-01-29 17:14  我就是隔壁老张  阅读(290)  评论(0编辑  收藏  举报