L2-027. 名人堂与代金券

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//一个构造体保存账号,分数
struct student  {
	string id;
	int score;
}; 
bool myCoop(student a, student b)  {
	if(a.score == b.score)
	    return a.id < b.id;
	else
	    return a.score > b.score;
}
int main()  {
	int N,G,K;
	cin>>N>>G>>K;
	student s[N];
	int money(0);
	for(int i = 0; i < N; i++)   {
		cin>>s[i].id>>s[i].score;
	    if(s[i].score >= 60 && s[i].score < G)
	        money += 20;
	    else if(s[i].score >= G)
	        money += 50;
	}
	cout<<money<<endl;
	sort(s, s+N, myCoop);
	int rank = 1;//排名
	int rerank = rank;  //上一个排名 
	cout<<rank<<" "<<s[0].id<<" "<<s[0].score<<endl;
	for(int i = 1; i < K; i++)  {
		if(s[i].score != s[i - 1].score)  {
			rank++;
			rerank = rank;
	        cout<<rerank<<" "<<s[i].id<<" "<<s[i].score<<endl;
	    }
	    else  {
	    	rank++;
	    	cout<<rerank<<" "<<s[i].id<<" "<<s[i].score<<endl;
		}
	}
	for(int j = K; j < N; j++)  {
	    if(s[j].score == s[K - 1].score)  {
	        cout<<rerank<<" "<<s[j].id<<" "<<s[j].score<<endl;
	    }
	    else
	        break;
	}
}

写一个结构体,新的排序,这些都不难,主要就是输出的时候,成绩相同的享有并列排名,比赛的时候没有完全做对。我的方法是用两个排名,一个排名是正常的排名(rank),只要输出就++,还有一个是上一个排名(renk),当分数和上一个相当时用rerank,否则更新rerank后再输出rerank
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值