Percy likes to be punctual. So much so that he always keeps three watches with him, so that he can be sure exactly what the time is. However, Percy's having a bad day. He found out that one of his watches was giving the wrong time. What's worse, when he went to correct the watch, he corrected the wrong one! That is, one watch was running x minutes behind (where x480) and he wound one of the other watches x minutes forward. He now has three watches reading three different times, and hence is in serious danger of being tardy. Can you help Percy by writing a program that takes in the three times displayed on the watches and returns the correct time?
Input
The input begins with an integer T indicating the number of cases that follow ( 0 < T < 100). Each of the following T lines contains one test case, made up of three readings, separated by single space characters:H1:M1 H2:M2 H3:M3 In each reading H1, H2, H3 represent the hours displayed ( 0 < H1, H2, H3 < 13), and M1, M2,M3 represent the minutes displayed ( 0M1, M2, M3 < 60).
If the number of minutes is less than 10, a leading 0 is perpended.
Output
For each test case, one line should be produced, formatted exactly as follows: "The correct time is Hi:Mi". If the number of minutes is less than 10, a leading 0 should be added. If the number of hours is less than 10, a leading 0 should NOT be added. If it is impossible to tell the time from the three readings, print the string: "Look at the sun".
Sample Input
3 5:00 12:00 10:00 11:59 12:30 1:01 12:00 4:00 8:00
Sample Output
The correct time is 5:00 The correct time is 12:30 Look at the sun
解题思路:x最大不过8小时,我们吧时间调到一个24小时内找三者中间点,若相差不是4小时即可。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int judge[8],sum[8],h[4],m[4],a[4];
int ab(int x)
{
if(x<0)
return -x;
return x;
}
int ji_suan(int x,int y,int z,int &k)
{
if(ab(x-y)==ab(y-z))
{
k = y;
return ab(x-y);
}
else if(ab(x-z)==ab(y-z))
{
k = z;
return ab(x-z);
}
else if(ab(x-y)==ab(z-x))
{
k = x;
return ab(x-y);
}
else
{
k = -1 ;
return -1;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int a[4];
for(int i=1; i<=3; i++)
scanf("%d:%d",&h[i],&m[i]);
for(int i=1; i<=3; i++)
a[i] = h[i]*60+m[i];
int flag = 0;
//24小时内换算
judge[1] = ji_suan(a[1],a[2],a[3],sum[1]);
judge[2] = ji_suan(a[1]+720,a[2],a[3],sum[2]);
judge[3] = ji_suan(a[1],a[2]+720,a[3],sum[3]);
judge[4] = ji_suan(a[1],a[2],a[3]+720,sum[4]);
judge[5] = ji_suan(a[1]-720,a[2],a[3],sum[5]);
judge[6] = ji_suan(a[1],a[2]-720,a[3],sum[6]);
judge[7] = ji_suan(a[1],a[2],a[3]-720,sum[7]);
for(int i=1; i<8; i++)
if(judge[i]>0)
{
flag = 1;
if(judge[i] == 240)//正好是一个环
{
printf("Look at the sun\n");
break;
}
else
{
printf("The correct time is %d:%02d\n",sum[i]/60,sum[i]%60);
break;
}
}
if(!flag)
printf("Look at the sun\n");
}
return 0;
}
/**
99
5:00 12:00 10:00
11:59 12:30 1:01
12:00 4:00 8:00
1:01 2:01 3:01
**/