Nine(2012 Rocky Mountain Regional Contest)

地址:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11346&courseid=0

Nine
Time Limit: 1500ms, Special Time Limit:3000ms, Memory Limit:32768KB
Total submit users: 7, Accepted users: 6
Problem 11346 : No special judgement
Problem description
   xkcd nine

Randall Munroe from xkcd.com pointed out that 9 is the most rarely used key on a microwave. Let's all share the load.

Given a desired cooking time, find a sequence of keys with the greatest number of 9's such that the resulting time has less than 10% error compared to the desired cooking time. In other words, if T is the desired cooking time in seconds, and T9 is the cooking time specified by the found sequence, then 10|T - T9| < T. If there are multiple possibilities, choose the one that has the smallest error (in magnitude). If there are still ties, choose the one that is lexicographically smallest.

For example, for T = 01:15, the times 00:68-00:82 and 1:08-1:22 have less than 10% error. Of these, 00:69, 00:79, 01:09, and 01:19 have the greatest number of 9's, and the ones with the smallest error are 00:79 and 01:19. The lexicographically smaller of these is 00:79.


Input
  

The input consists of a number of cases. For each case, the desired cooking time in MM:SS format is specified on one line. Each M or S can be any digit from 0 to 9. The end of input is indicated by 00:00.


Output
  

For each case, output on a single line the four keys to use as input to the microwave, in MM:SS format.


Sample Input
00:30
01:00
02:00
91:30
46:03
00:00
Sample Output
00:29
00:59
01:59
90:99
49:99

题意:输入一个时间点,照在误差小于时间值的10%的一段时间内含9最多的时间点,如果遇到含9数目相同的则选与输入时间点的差值小的,如果差值一样,就选字典序小的

易错例子:09:34

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
	int m,s;
}ti[4];
int textnine(node tk)
{
	int sum=0;
	if(tk.m/10==9) sum++;
	if(tk.m%10==9) sum++;
	if(tk.s/10==9) sum++;
	if(tk.s%10==9) sum++;
	return sum;
}
node changen(node tk)
{
	node ts;
	ts=tk;
	if(tk.s<40&&tk.m>0)
	{
		ts.m--;
		ts.s+=60;
	}
	return ts;
}
int main()
{
	node ans,gin;
	int i,j,m,max9,k,c;
	while(scanf("%d:%d",&gin.m,&gin.s)>0)
	{
		if(!gin.m&&!gin.s) break;
		gin.m+=gin.s/60;
		gin.s%=60;
		max9=textnine(gin);
		ans=gin;c=0;
		ti[0]=changen(gin);
		k=textnine(ti[0]);
		if(k>max9)
		{
			max9=k;
			ans=ti[0];
		}
		else if(k==max9)
		{
			if(ans.m>ti[0].m)
			{
				ans.m=ti[0].m;
				ans.s=ti[0].s;
			}
			else if(ans.m==ti[0].m&&ans.s>ti[0].s)
				ans.s=ti[0].s;
		}
		m=(gin.m*60+gin.s)/10;
		if(gin.s%10==0) m--;
		for(i=1;i<=m;i++)
		{
			ti[0]=gin;
			ti[0].m+=(ti[0].s-i)/60;
			ti[0].s=(ti[0].s-i)%60;
			if(ti[0].s<0) {ti[0].m--;ti[0].s+=60;}
			ti[1]=changen(ti[0]);
			ti[2]=gin;
			ti[2].m+=(ti[2].s+i)/60;
			ti[2].s=(ti[2].s+i)%60;
			ti[3]=changen(ti[2]);
			for(j=0;j<4;j++)
			{
				k=textnine(ti[j]);
				if(k>max9)
				{
					max9=k;c=i;
					ans=ti[j];
				}
				else if(k==max9&&i==c)
				{
					if(ans.m>ti[j].m) ans=ti[j];
					else if(ans.m==ti[j].m&&ans.s>ti[j].s)
						ans.s=ti[j].s;
				}
			}
		}
		printf("%02d:%02d\n",ans.m,ans.s);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值