文章目录
模板
考排序,有两种情况
- 考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;