地址: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 |
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;
}