Click to open hdu1209
Problem Descriptionthere is an analog clock with the Hands:an hour hand and a minute hand. The hands form an angle. The angle is measured as the smallest angle between. The angle between the hands have a measure that's greater than or equal to 0 and less than or equal to degrees.
Given a sequence of five distinct times written in the format hh:mm, where HH is both digits representing full hours (0 0 <= hh <=) and mm are both digits representing minutes (xx <= mm <=), you is to write a program that F Inds The median, that's, the third element of the sorted sequence of times in a nondecreasing order of their associated a Ngles. Ties is broken in such a, a earlier time precedes a later time.
For example, suppose is given a sequence (06:05, 07:10, 03:00, 21:00, 12:55) of the Times. Because the sorted sequence is (12:55, 03:00, 21:00, 06:05, 07:10), and you were to report 21:00.
Inputthe 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 was given on a, which contains a sequence of five distinct times, where times was given in the F Ormat hh:mm and is separated by a single space.
Outputprint exactly one line for each test case. The 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
idea: The main problem is to find the size of the clock and minute angle, and then to sort, but also pay attention to a small detail problem, when the angle is equal, the time is small in front.
Angle Seeking method : minute rotation of 60 minutes a week, so minute rotation 360 degrees per minute divided by 60, 6 degrees, and clock to 12 hours a week, an hour equals 60 minutes, so the speed of the minute rotation is 12 times times the clock rotation, that is, the clock rotation speed of 0, 5 degrees per minute.
Thus the formula R=h*30+0.5*m-m*6 (h*30: Represents the degree of the clock, and 0.5*m: For the rotation of M minutes, the degree of rotation of the clock, the sum of which is the total rotation of the clock angle, m*6 is the angle of the minute rotation)
Import Java.util.scanner;public class P1209 {public static void main (string[] args) {Scanner sc=new Scanner (system.in); T T=sc.nextint (); while (t-->0) {String s; String[] Str;int h,m; Time[] time=new time[5];for (int i=0;i<5;i++) {s=sc.next (); Str=s.split (":"); H=integer.parseint (str[0]); m= Integer.parseint (str[1]); time[i]=new time (h,m);} Sort (time); System.out.printf ("%02d:%02d", TIME[2].H,TIME[2].M); System.out.println ();}} private static void sort (time[] time) {for (int. i=0;i<time.length-1;i++) {for (int j=0;j<time.length-1-i;j++) {if ( TIME[J].R>TIME[J+1].R) {swap (time,j,j+1);} else if (TIME[J].R==TIME[J+1].R) {if (time[j].h>time[j+1].h) {swap (time,j,j+1);}}}} private static void Swap (time[] time, int J, int i) {time t=new time (); t=time[j];time[j]=time[i];time[i]=t;}} class Time{public int h;public int m;public double r;public time (int h,int m) {this.h=h;this.m=m;setr ();} private void Setr () {this.r=math.abs (h%12*30.0+m*0.5-m*6.0); if (this.r>180) {THIS.R=360-THIS.R;}} Public time () {}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu1209 (Clock)