HDU-5705

Clock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

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(0HH<12,0MM<60,0SS<60).
The second line contains one integer a(0a180).
 
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  
 
Source
 
题解:给出一个当前的时间和角度a,问从现在开始的下一个时针和分针形成角度a的时间是多少,时间向下取整。
 
思路:时针3600s走30°,故120s走1°,分针3600s走360°,故10s走1°,那么每过120s它们就会相差11°,即每过120/11s相差1°,因此设tar是从0:00:00到当前的时间所经过的秒数,cnt也是一样,但是cnt的初始值是a°乘以120/11s,也就是说它是从0:00:00开始的第一个所能形成角度a的时间,然后不断地变更到下一个角度为a的时间即可(所有的时间都先用秒来表示,最后换算成时间),直到超过了tar,那么它就是第一个过了当前时间的并且形成角度a的时间了。但是要注意的一个问题就是,乘以一个分数可能会出现精度问题(事实上在这题它的确出现了),所以把1秒钟再拆成11个小单位,也就是说把所有的时间都换算成更小的单位(即乘以11),这样就可以避免精度问题了。
(来源:https://www.cnblogs.com/zzyDS/p/5539014.html)
 
代码:
 1 #include <stdio.h>
 2 int main()
 3 {
 4     int h,m,s,a,da,tots=12*3600*11,kase=1;
 5     while(scanf("%d:%d:%d",&h,&m,&s)==3)
 6     {
 7         int tar = 11*(h*3600+m*60+s);
 8         scanf("%d",&a);
 9         da = 360 - 2*a;
10         int cnt = a*120, f = 1;
11         //一开始的位置在它们相差了a°的位置(显然第一次是分针在前)
12         while(cnt<=tar)
13         {
14             if(f)
15             {
16                 f=0;
17                 cnt+=da*120;//第一次追及是分针追(360-2*a)的角度,以再一次形成相差a°的情况
18                             //这一次是分针在时针前面a°
19             }
20             else
21             {
22                 f=1;
23                 cnt+=2*a*120;//再追2*a°,分针又在时针前a°
24                              //此后一直循环往复直到超过当前时间
25             }
26         }
27         if(cnt>=tots) cnt-=tots;
28         int ansh,ansm,anss;
29         ansh = cnt/(3600*11);
30         cnt%=3600*11;
31         ansm=cnt/(60*11);
32         cnt%=60*11;
33         anss=cnt/11;
34         printf("Case #%d: %02d:%02d:%02d
",kase++,ansh,ansm,anss);
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/Tangent-1231/p/8067626.html