题目描述
Jamie loves sleeping. One day, he decides that he needs to wake up at exactly hh:mm. However, he hates waking up, so he wants to make waking up less painful by setting the alarm at a lucky time. He will then press the snooze button every x minutes until hh:mm is reached, and only then he will wake up. He wants to know what is the smallest number of times he needs to press the snooze button.
A time is considered lucky if it contains a digit '7'. For example, 13:07 and 17:27 are lucky, while 00:48 and 21:34 are not lucky.
Note that it is not necessary that the time set for the alarm and the wake-up time are on the same day. It is guaranteed that there is a lucky time Jamie can set so that he can wake at hh:mm.
Formally, find the smallest possible non-negative integer y such that the time representation of the time x·y minutes before hh:mm contains the digit '7'.
Jamie uses 24-hours clock, so after 23:59 comes 00:00.
A time is considered lucky if it contains a digit '7'. For example, 13:07 and 17:27 are lucky, while 00:48 and 21:34 are not lucky.
Note that it is not necessary that the time set for the alarm and the wake-up time are on the same day. It is guaranteed that there is a lucky time Jamie can set so that he can wake at hh:mm.
Formally, find the smallest possible non-negative integer y such that the time representation of the time x·y minutes before hh:mm contains the digit '7'.
Jamie uses 24-hours clock, so after 23:59 comes 00:00.
输入
The first line contains a single integer x (1≤ x≤ 60).
The second line contains two two-digit integers, hh and mm (00≤ hh≤ 23, 00 ≤ mm≤ 59).
The second line contains two two-digit integers, hh and mm (00≤ hh≤ 23, 00 ≤ mm≤ 59).
输出
Print the minimum number of times he needs to press the button.
样例输入
3
11 23
样例输出
2
提示
In the first sample, Jamie needs to wake up at 11:23. So, he can set his alarm at 11:17. He would press the snooze button when the alarm rings at 11:17 and at 11:20.
题目大意:给定一个起床时间hhmm和每隔x分钟按一次闹钟,要你找到一个hhmm之前的幸运时刻使得起床前按闹钟的次数最少(从幸运时刻开始到起床时间结束按闹钟的次数最少)
这个题,因为涉及到时刻转化的问题,所以很容易错,需要考虑周到。
如下测试用例:
7
11 17(应输出0)
60
00 00 (应输出7)
7
20 50(应输出9)
基本思路还是很简单,就是将起床时间不断减小x分钟,并记录减了几次,直到一个幸运时刻为止,答案就是减的次数。(当然减的过程不能出现负数,需转化一下,容易出错)
AC代码:
#include<cstdio> bool islucky(int m){ if(m%10==7||m/10==7) return true; return false; } int main() { int x; scanf("%d",&x); int h,m; scanf("%d%d",&h,&m); int ans=0; int sgn=0; if(islucky(h)||islucky(m)) { printf("0 ");//如果hhmm就是幸运数,直接输出0就好了 return 0; } while(!sgn){ if(m<=0&&h>=1){ m+=60; h-=1; if(islucky(h)||islucky(m)){ if(m==60) ans++;//这个很重要,因为如果没有这句,如18:01减1后变到18:00也就是17:60这样的只算1次减就直接break掉了。 sgn=1; break; } } else if(m<=0&&h<=0){ m+=60; h=23; if(islucky(h)||islucky(m)){ if(m==60) ans++;//同上 sgn=1; break; } } else if(m>0){ m-=x; ans++; if(islucky(h)||islucky(m)){ sgn=1; break; } } } printf("%d ",ans); return 0; }
总结:有很多细节要考虑,怎么说呢,多用极端用例测测吧,比如令x=60,令hhmm=00 00,同时令x=60 hhmm=00 00( = =!)