HDU--1236:排名 (水题)

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从小到大排序 
}

总之,注意一下这两者用法。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值