Clock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2985 Accepted Submission(s): 914
Total Submission(s): 2985 Accepted Submission(s): 914
Problem Description
There is an analog clock with two hands: an hour hand and a minute hand. The two hands form an angle. The angle is measured as the smallest angle between the two hands. The angle between the two hands has a measure that is greater than or equal to 0 and less than or equal to 180 degrees.
Given a sequence of five distinct times written in the format hh : mm , where hh are two digits representing full hours (00 <= hh <= 23) and mm are two digits representing minutes (00 <= mm <= 59) , you are to write a program that finds the median, that is, the third element of the sorted sequence of times in a nondecreasing order of their associated angles. Ties are broken in such a way that an earlier time precedes a later time.
For example, suppose you are given a sequence (06:05, 07:10, 03:00, 21:00, 12:55) of times. Because the sorted sequence is (12:55, 03:00, 21:00, 06:05, 07:10), you are to report 21:00.
Given a sequence of five distinct times written in the format hh : mm , where hh are two digits representing full hours (00 <= hh <= 23) and mm are two digits representing minutes (00 <= mm <= 59) , you are to write a program that finds the median, that is, the third element of the sorted sequence of times in a nondecreasing order of their associated angles. Ties are broken in such a way that an earlier time precedes a later time.
For example, suppose you are given a sequence (06:05, 07:10, 03:00, 21:00, 12:55) of times. Because the sorted sequence is (12:55, 03:00, 21:00, 06:05, 07:10), you are to report 21:00.
Input
The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case is given on a single line, which contains a sequence of five distinct times, where times are given in the format hh : mm and are separated by a single space.
Output
Print exactly one line for each test case. The line is to contain the median in the format hh : mm of the times given. The following shows sample input and output for three test cases.
Sample Input
3 00:00 01:00 02:00 03:00 04:00 06:05 07:10 03:00 21:00 12:55 11:05 12:05 13:05 14:05 15:05
Sample Output
02:00 21:00 14:05
——————————————————————————————————————————————————————————————————
注意时间的计算方法,时针是跟着分针动的,比如7:30的角度不是7点的角度和30分的角度只差,小时角度的计算还要加上分针的偏移量
还有当角度相同时,时间小的在前面
/***********************
* 程序名:clock.cpp
* 功能:ACM
************************/
#include <stdio.h>
#include <string.h>
void PubbleSort(double (*)[3], int);
int main()
{
int T;
char time[5][5], string[30], c;
double sequence[5][3];
int i, j, t;
double hour, min, degree;
scanf("%d", &T);
c=getchar();
while(T--) {
gets(string);
t = 0;
for(i = 0; i < 5; i++) {
for(j = 0; j < 5; j++) {
time[i][j] = string[t++];
}
t++;
}
for(i = 0; i < 5; i++) {
hour = (time[i][0]-'0')*10+time[i][1]-'0';
min = (time[i][3]-'0')*10+time[i][4]-'0';
sequence[i][2] = hour;
if(hour > 12) {
hour -= 12;
}
if((hour+min/60.0)*30 > min*6) {
degree = (hour+min/60.0)*30-min*6;
}
else {
degree = min*6-(hour+min/60.0)*30;
}
if(degree > 180) {
degree = 360 - degree;
}
sequence[i][0] = degree;
sequence[i][1] = i;
}
PubbleSort(sequence, 5);
i = sequence[2][1];
for(j = 0; j < 5; j++) {
printf("%c", time[i][j]);
}
printf("\n");
}
return 0;
}
void PubbleSort(double (*array)[3], int n)
{
int i, j;
double temp_1, temp_2, temp_3;
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(array[j][0] >= array[j+1][0]) {
if(array[j][0] == array[j+1][0]) {
if(array[j][2] > array[j+1][2]) {
temp_1 = array[j][0];
temp_2 = array[j][1];
temp_3 = array[j][2];
array[j][0] = array[j+1][0];
array[j][1] = array[j+1][1];
array[j][2] = array[j+1][2];
array[j+1][0] = temp_1;
array[j+1][1] = temp_2;
array[j+1][2] = temp_3;
}
}
else {
temp_1 = array[j][0];
temp_2 = array[j][1];
temp_3 = array[j][2];
array[j][0] = array[j+1][0];
array[j][1] = array[j+1][1];
array[j][2] = array[j+1][2];
array[j+1][0] = temp_1;
array[j+1][1] = temp_2;
array[j+1][2] = temp_3;
}
}
}
}
/*for(i = 0; i < 5; i++) {
printf("degree=%.0lf hour=%.0lf ", array[i][0], array[i][2]);
}
printf("\n");*/
}