HDU1177做题笔记

题目链接: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;
		
	}
	
}

【总结】挺水的一题,就是代码有点长,会使用结构体并且排序的话应该就挺方便的。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值