HDU1209 ZOJ2680 UVALive2807 Clock【排序】
程序员文章站
2024-01-23 20:02:46
...
Clock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6451 Accepted Submission(s): 2027
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
300:00 01:00 02:00 03:00 04:0006:05 07:10 03:00 21:00 12:5511:05 12:05 13:05 14:05 15:05
Sample Output
02:0021:0014:05
Source
问题链接:HDU1209 ZOJ2680 UVALive2807 Clock
问题简述:
输入有多个样例,每个样例有5个时间(小时:分钟),按时间的夹锐角从小到大进行排序,若夹角相等则按时间顺序。输出排序后位于中间的时间。
问题分析:
夹角求法:分针60分钟旋转一周,分针每1分钟旋转360/60=6度;时针12小时旋转一周,每1小时等于60分钟,分针转动速度是时针的12倍,即时针转动速度为每分钟6/12=0.5度。得等公式a=h*30+m/2-m*6。其中:
h*30表示时针准点度数;
m/2表示分针转动m分钟所转动的度数;
m*6表示分针转动的度数。
程序说明:(略)
题记:(略)
参考链接:(略)
/* HDU1209 ZOJ2680 UVALive2807 Clock */
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
const int N = 5;
struct Node {
int h, m;
double a;
} clock2[N];
int cmp(Node x, Node y)
{
if(x.a != y.a)
return x.a < y.a;
else
return x.h < y.h;
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
for(int i = 0; i < N; i++) {
scanf("%d:%d", &clock2[i].h, &clock2[i].m);
if(clock2[i].h > 12)
clock2[i].a = fabs(30.0 * (clock2[i].h - 12) + clock2[i].m / 2.0 -6.0 * clock2[i].m);
else
clock2[i].a = fabs(30.0 * clock2[i].h + clock2[i].m / 2.0 - 6.0 * clock2[i].m);
if(clock2[i].a > 180)
clock2[i].a = 360 - clock2[i].a;
}
sort(clock2, clock2 + N, cmp);
printf("%02d:%02d\n", clock2[2].h, clock2[2].m);
}
return 0;
}
上一篇: clock()