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 theresulting time has less than 10% error compared to the desired cooking time. In other words, if Tis 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 smallesterror (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 withthe 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:SSformat is specified on one line. Each M or S can be any digit from 0 to 9. The end of input isindicated by `00:00'.
Output
For each case, output on a single line the four keys to use as input to the microwave, inMM: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%以内(不包括边界值)的以下条件依次满足的时间输出:
(1)含9最多的时间输出
(2)如果含9数目相同,则选误差小的时间输出
(3)如果误差一样,则选字典序小的输出
(4)如果在误差范围内没有含9数目为0的,则输出原来的时间。
AC代码:
#include <stdio.h>
int ff(int a,int b){
int c;
c=a-b;
if(c<0)
c=-c;
return c;
}
int gg(int a,int b){
int sum=0;
if(a%10==9)
sum++;
if((a/10)%10==9)
sum++;
if(b%10==9)
sum++;
if((b/10)%10==9)
sum++;
return sum;
}
int main(){
int a,b,s,s1,s2,m,max,m1,m2,k,j,i,p,flag,q,w,z,x;
while(~scanf("%d:%d",&a,&b)){
if(!a&&!b)
break;
s=a*60+b;
m=s/10;
if(s%10)
m++;
s1=s-m;//误差的下限
s2=s+m;//误差的上限
max=-1;
q=a;
w=b;
for(i=s1+1;i<s2;i++){//在误差范围内暴力枚举
m1=m2=0;
k=i/60;
j=i%60;
m1=gg(k,j);//没有向分借1,含9的数目
if(j<=39&&k){//向分借1,秒+60,含9的数目
k--;
j+=60;
m2=gg(k,j);
}
flag=0;
if(m1>m2)
flag=1;
els{
if(m1==m2&&m1==0)//考虑到含9的数目为0时要输出原型
flag=1;
m1=m2;
}
if(max<m1){
max=m1;
p=i;
if(flag){
q=i/60;
w=i%60;
}
else{
q=i/60-1;
w=i%60+60;
}
}
else if(max==m1){
if(ff(i,s)<ff(p,s)){//考虑误差大小
max=m1;
p=i;
if(flag){
q=i/60;
w=i%60;
}
else{
q=i/60-1;
w=i%60+60;
}
}
else if(ff(i,s)==ff(p,s)){//考虑字典序
if(flag){
z=i/60;
x=i%60;
}
else{
z=i/60-1;
x=i%60+60;
}
if(q==z){
if(x<w){
q=z;
w=x;
p=i;
}
}
if(q>z){
q=z;
w=x;
p=i;
}
}
}
}
printf("%02d:%02d\n",q,w);
}
return 0;
}