HDU-5705 Clock
Problem Description
Given a time HH:MM:SS and one parameter a, you need to calculate next time satisfying following conditions:
1.The angle formed by the hour hand and the minute hand is a.
2.The time may not be a integer(e.g. 12:34:56.78), rounded down(the previous example 12:34:56).
Input
The input contains multiple test cases.
Each test case contains two lines.
The first line is the time HH:MM:SS(0≤HH<12,0≤MM<60,0≤SS<60).
The second line contains one integer a(0≤a≤180).
Output
For each test case, output a single line contains test case number and the answer HH:MM:SS.
Sample Input
0:59:59
30
01:00:00
30
Sample Output
Case #1: 01:00:00
Case #2: 01:10:54
题目大意:
给你一个时刻 t t t 和一个角度 θ \theta θ ,问你时针和分针在时刻 t t t 过后(不包括 t t t )第一次到达 θ \theta θ 角的时刻。
这题好坑啊,WA了好几遍,最后发现是因为题目是按12小时制算的,而我是按24小时制算的,知道真相的我眼泪掉下来,23333。
一开始我打算计算时针和分针现在的角度,然后计算从现在的角度到 θ \theta θ 角需要多长时间。这个方法贼复杂,而且容易有误差。后来我发现其实没必要。我们只关心角度到达 θ \theta θ 的时刻,而不关心经过的时间。
所以,我们只需要求出在时针和分针达到θ角的时刻中,第一个大于 t t t 的时刻即可。由于第一个大于 t t t 的时刻的 h h h (小时数)一定大于等于初始时刻 t t t 的 h h h ,所以我们只需要从 h : 00 : 00 h:00:00 h:00:00开始求即可。
h : 00 : 00 h:00:00 h:00:00时刻,时针在分针前面30* h h h度。经过 s s s 秒后,时针转了 s 120 \frac{s}{120} 120s度,分针转了 s 10 \frac{s}{10} 10s度。即,分针比时针多转 11 s 120 \frac{11s}{120} 12011s度。然后两指针成 θ \theta θ 角有两种情况:时针在前,分针在前。这两种情况的角度差值分别为 a + 360 k a+360k a+360k, a − 360 k ( k ∈ Z ) a-360k(k\in\mathbb{Z}) a−360k(k∈Z) 。而分针和时针的差值增加360度所需的时间是固定的 360 × 11 120 \frac{360 \times 11}{120} 120360×11,所以我们只要不断将时间加上这个常数,直至出现第一个大于 t t t 的时刻为止,然后将其转化为时刻。记得取模的时候是模12,血的教训。。。。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int hh,mm,ss,tmp,cnt=0;
double h,m,s,t,o,a,tt,p,tmp1,tmp2,c;
c=360.0*120.0/11.0;
while(scanf("%lf:%lf:%lf",&h,&m,&s)!=EOF){
scanf("%lf",&a);
t=60.0*m+s;
o=30.0*h;
p=o+a,o=o-a;
if(o<0) o+=360.0;
tmp1=o*120.0/11.0;
tmp2=p*120.0/11.0;
if(tmp1>tmp2) swap(tmp1,tmp2);
while(true){
if(tmp1>t) {tt=tmp1;break;}
else if(tmp2>t) {tt=tmp2;break;}
tmp1+=c,tmp2+=c;
}
ss=tt,hh=h,mm=0;
tmp=ss/60,ss%=60;
mm+=tmp,tmp=mm/60,mm%=60;
hh+=tmp,hh%=12;
printf("Case #%d: ",++cnt);
printf("%02d:%02d:%02d\n",hh,mm,ss);
}
}