PAT之排序

模板

考排序,有两种情况

  • 考sort()函数,再结合其他
  • 考排序算法
    题目会介绍哪种排序算法的过程,需要你实现排序算法(已考过:简单选择、简单插入、堆排、快排)
    这次2020春季最后一道30分题,就是介绍一种外部排序算法,需要自己实现

1012(25:列排序)

(1)题目
各科排名,每列

(2)代码

#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

const int N = 1000000+10;

char course[5] = {'A','C','M','E'};

typedef struct Node{
	int id;
	double C;
	double M;
	double E;
	double A;
	int index;		//最好排名对应的课程index
	int r;
}Node;

vector<Node> vect;
int visit[N];
int tempRank[N];

bool cmp_A(Node no1, Node no2){
	return no1.A > no2.A;
}

bool cmp_C(Node no1, Node no2){
	return no1.C > no2.C;
}

bool cmp_M(Node no1, Node no2){
	return no1.M > no2.M;
}

bool cmp_E(Node no1, Node no2){
	return no1.E > no2.E;
}
	
int main(){
	//freopen("in.txt", "r",stdin);

	int n,m;
	scanf("%d%d", &n, &m);
	Node node;
	for(int i=0; i<n; i++){
		int id;
		scanf("%d", &id);
		double c,m,e,a;
		scanf("%lf%lf%lf", &c, &m, &e);
		node.id = id;
		node.C = c;
		node.M = m;
		node.E = e;
		node.A = (c+m+e)/3;
		vect.push_back(node);
		visit[id] = 1;
	}
	int cnt=1;
	sort(vect.begin(), vect.end(), cmp_A);
	for(int i=0; i<vect.size(); i++){
		if(i!=0){
			if(vect[i].A == vect[i-1].A){
				tempRank[vect[i].id] = tempRank[vect[i-1].id];				
			}else{
				tempRank[vect[i].id] = cnt;
			}
		}else{
			tempRank[vect[i].id] = 1;
		}
		cnt++;
		vect[i].r = tempRank[vect[i].id];
		vect[i].index = 0;
	}
	cnt=1;
	sort(vect.begin(), vect.end(), cmp_C);
	for(int i=0; i<vect.size(); i++){
		if(i!=0){
			if(vect[i].C == vect[i-1].C){
				tempRank[vect[i].id] = tempRank[vect[i-1].id];				
			}else{
				tempRank[vect[i].id] = cnt;
			}
		}else{
			tempRank[vect[i].id] = 1;
		}
		cnt++;
		if(tempRank[vect[i].id] < vect[i].r){
			vect[i].r = tempRank[vect[i].id];
			vect[i].index = 1;
		}		
	}
	cnt=1;
	sort(vect.begin(), vect.end(), cmp_M);
	for(int i=0; i<vect.size(); i++){
		if(i!=0){
			if(vect[i].M == vect[i-1].M){
				tempRank[vect[i].id] = tempRank[vect[i-1].id];				
			}else{
				tempRank[vect[i].id] = cnt;
			}
		}else{
			tempRank[vect[i].id] = 1;
		}
		cnt++;
		if(tempRank[vect[i].id] < vect[i].r){
			vect[i].r = tempRank[vect[i].id];
			vect[i].index = 2;
		}	
	}
	cnt=1;
	sort(vect.begin(), vect.end(), cmp_E);
	for(int i=0; i<vect.size(); i++){
		if(i!=0){
			if(vect[i].E == vect[i-1].E){
				tempRank[vect[i].id] = tempRank[vect[i-1].id];				
			}else{
				tempRank[vect[i].id] = cnt;
			}
		}else{
			tempRank[vect[i].id] = 1;
		}
		cnt++;
		if(tempRank[vect[i].id] < vect[i].r){
			vect[i].r = tempRank[vect[i].id];
			vect[i].index = 3;
		}		
	}
	for(int i=0; i<m; i++){
		int id;
		scanf("%d", &id);
		if(visit[id]==0){
			printf("N/A\n");
			continue;
		}
		for(int i=0; i<vect.size(); i++){
			if(id==vect[i].id){
				printf("%d %c\n", vect[i].r, course[vect[i].index]);
			}
		}
	}

	//fclose(stdin);
	return 0;
}

(3)小结

  • 排名并列
    1 1 1 4 5 6…
  • 结构体设计
typedef struct Node{
	int id;
	double C;
	double M;
	double E;
	double A;
	int index;		//最好排名对应的课程index
	int r;
}Node;

应该用数组

typedef struct Node{
	int id;
	double score[4];
	int rank[4];
}Node;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值