1. 题目源地址:http://acm.hdu.edu.cn/showproblem.php?pid=1236
2. 程序源代码:
//HOJ--1236:排名
#include<iostream>
#include<string>
#include<algorithm>
#include<memory.h>
using namespace std;
struct node
{
string ID;
int score;
}people[1010];
int cmp(node a,node b)
{
if(a.score != b.score)
return a.score > b.score;//优先按照分数从大到小排序
else
return a.ID < b.ID;//分数相同,则按照ID从小到大排序
}
int main()
{
int N,M,line;
int i,j;
int p[15];//存放每道题得分的数组
int solved,proID;//solved:学生解决题的道数 proID: 题目的编号
while(cin>>N)
{
if(N==0) break;
cin>>M>>line;
memset(p,0,sizeof(p));
for(i=1;i<=M;i++)
cin>>p[i];
for(i=0;i<N;i++)
{
cin>>people[i].ID;
cin>>solved;
people[i].score=0;
for(j=1;j<=solved;j++)
{
cin>>proID;
people[i].score+=p[proID];
}
}
sort(people,people+N,cmp);
int sum=0;//超过分数线的考生数目
for(i=0;i<N;i++)
{
if(people[i].score>=line)
sum++;
}
cout<<sum<<endl;//输出超过分数线的考生数量
//因为经过排序,则数组钱sum个学生都是过线的,直接输出就行
for(i=0;i<sum;i++)
cout<<people[i].ID<<" "<<people[i].score<<endl;
}
//system("pause");
return 0;
}
3. 注意:
(1)此题ID使用了string类型,排序函数为:
struct node
{
string ID;
int score;
}people[1010];
int cmp(node a,node b)
{
if(a.score != b.score)
return a.score > b.score;//优先按照分数从大到小排序
else
return a.ID < b.ID;//分数相同,则按照ID从小到大排序
}
(2)ID也可以使用字符数组类型,排序函数为:
struct node
{
char ID[25];
int score;
}people[1010];
int cmp(node a,node b)
{
if(a.score != b.score)
return a.score > b.score;//优先按照分数从大到小排序
else
return strcmp(a.ID,b.ID)<0;//分数相同,则按照ID从小到大排序
}
总之,注意一下这两者用法。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/163050.html