L2-027. 名人堂与代金券
2018-04-17 23:05
239 查看
#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 ; 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 阅读更多
相关文章推荐
- gplt L2-027. 名人堂与代金券(水题 l2 最简单)
- L2-027. 名人堂与代金券【结构体排序】
- L2-027. 名人堂与代金券
- PAT L2-027. 名人堂与代金券
- L2-027. 名人堂与代金券
- 练习集 L2-027 名人堂与代金券(25 分)(排序+排名)
- L2-027. 名人堂与代金券,结构体排序
- PAT L2-027 名人堂与代金券(25 分)
- 天梯赛 名人堂与代金券
- CCCC L2-025 L2-026 L2-027
- [IT名人堂] 编译器优化奠基人:John Cocke
- 《给初学者的Windows Vista的补遗手册》之027
- 【奥运名人堂】安德森
- 【奥运名人堂】贝雷斯福德
- 列和相减 L3-L2
- AIX:0403-027 参数列表过长
- 思科L3 L2 IOU模拟器 迁移至RHEL 6.1
- java经典算法_027取一个整数a从右端开始的4~7位。
- (转载)老 I讲硬件故事——L1、L2、内存和硬盘的故事