题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1177
【思路】
给所有人进行排名,然后最后输出某一个人获得的奖项(看了半天才知道第一行最后一个参数是什么意思,原来是指定了输出哪个人)。这是四种奖项金、银、铜、鼓励。(应该是这四个奖吧,没参加过)
Accepted today? I've got a golden medal :)
Accepted today? I've got a silver medal :)
Accepted today? I've got a copper medal :)
Accepted today? I've got an honor mentioned :)
主要用到的是结构体,来存储记录顺序、解决题数、用时以及奖项。同时还用到了sort,来对结构体进行一定规则的排序。
其次我在输入做了一点修改,自从上次看过别人输入时间的方式后,就采用了scanf("%d:%d:%d",hh,mm,ss)
进行时间的输入,这样可以直接获得三个整数,还是挺方便的嗷。
不过用cin
的话可以将时间作为一整个字符串存储,然后在cmp1函数
内比较时间的时候用字符串的比较方式也可以(strcmp
);也可以把字符串的数字位与’0’做减法,然后获得整型数字。(注意是有单引号的0)
【代码】
#include<bits/stdc++.h>
using namespace std;
struct programmar{
int id;
int p;
int time;
int ward;
}pg[150];
bool cmp1(programmar a,programmar b)
{
if(a.p>b.p)return true;
else if(a.p==b.p)
{
if(a.time<b.time)
return true;
else return false;
}
return false;
}
bool cmp2(programmar a,programmar b)
{
if(a.id<b.id)
return true;
return false;
}
int main()
{
int n,g,s,c,m,hh,mm,ss;
while(cin>>n>>g>>s>>c>>m&&n!=0)
{
for(int i=0;i<n;i++)
{
cin>>pg[i].p;
pg[i].id=i;
scanf("%d:%d:%d",&hh,&mm,&ss);//输入时间
pg[i].time=hh*3600+mm*60+ss;
}
sort(pg,pg+n,cmp1);
for(int j=0;j<n;j++)
{
if(g>0){
pg[j].ward=1;
g-=1;
continue;
}
else if(s>0){
pg[j].ward=2;
s-=1;
continue;
}
else if(c>0){
pg[j].ward=3;
c-=1;
continue;
}
else {
pg[j].ward=4;
continue;
}
}
sort(pg,pg+n,cmp2);//按id排序
if(pg[m-1].ward==1)
cout<<"Accepted today? I've got a golden medal :)"<<endl;
if(pg[m-1].ward==2)
cout<<"Accepted today? I've got a silver medal :)"<<endl;
if(pg[m-1].ward==3)
cout<<"Accepted today? I've got a copper medal :)"<<endl;
if(pg[m-1].ward==4)
cout<<"Accepted today? I've got an honor mentioned :)"<<endl;
}
}
【总结】挺水的一题,就是代码有点长,会使用结构体并且排序的话应该就挺方便的。