Clock

来源:互联网 发布:php是最好的语言 编辑:程序博客网 时间:2024/05/21 15:50
Given a time HH:MM:SS and one parameter aa, you need to calculate next time satisfying following conditions: 

1. The angle formed by the hour hand and the minute hand is aa. 
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)(0≤HH<12,0≤MM<60,0≤SS<60). 
The second line contains one integer a(0a180)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:593001:00:0030
Sample Output

Case #1: 01:00:00Case #2: 01:10:54

题意:给一个当前时间和一个角度,要求输出时针和分针成当前角度的一个最近时刻

首先我们推算出,秒针每走1°,分针走1/10°,时针走1/120°,所以干脆全部乘120,得到120->12->1,因此我们把120当作1°,所以只要将360*120当作秒针走一周的度数就可以处理,这样处理的优点是排除误差

接下来就可以暴力求解出几秒后可以得到解,因为我们对度数处理过,所以每过1s,时针度数+1,分针度数+12,判断一下当前锁形成的角度即可

代码如下

#include<stdio.h>#include<iostream>#include<math.h>using namespace std;int abs(int a){    if(a<0)        return -a;        else            return a;}int main(){    int a,b,c;    int g=0;    while(scanf("%d:%d:%d",&a,&b,&c)!=-1)    {        ++g;        int we;        scanf("%d",&we);        we=we*120;        int sum=a*3600+b*60+c;        int hh=sum%(360*120);        int mm=(sum*12)%(360*120);        int ans=0;        while(1)        {            ans++;            hh=(hh+1)%(360*120);            mm=(mm+12)%(360*120);            if(abs(abs(hh-mm)-we)<=10) break;        }        printf("Case #%d: ",g);       int ansc=(ans+c)%60;       int ansm=(b+(ans+c)/60)%60;       int ansh=(a+((((ans+c)/60)+b)/60))%12;        printf("%02d:%02d:%02d\n",ansh,ansm,ansc);    }}


0 0