Hdu1209 (Clock), hdu1209clock

Source: Internet
Author: User

Hdu1209 (Clock), hdu1209clock

Click to open hdu1209

Problem DescriptionThere is an analog clock with two hands: an hour hand 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 writing 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 (,) of times. because the sorted sequence is (,), you are to report.
 
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 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.
 
OutputPrint 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

Idea: This is mainly to find the angle between the clock and the minute, and then sort it, but also pay attention to a small detail. When the angle is equal, put the time in front.

Method of angle calculation: minute rotation takes 60 minutes for a week, so minute rotation is 360 degrees divided by 60 degrees, which is 6 degrees, and clock conversion is 12 hours, one hour equals 60 minutes, therefore, the minute rotation speed is 12 times the clock rotation speed, that is, the clock rotation speed is degrees per minute.

The formula r = h * 30 + 0.5 * m-m * 6 (h * 30: indicates the degree of the on-time clock, and 0.5 * m: indicates that when the m minute is turned, the degrees of clock rotation. The sum of the two is the overall Rotation Angle of the clock, and m * 6 is the Rotation Angle of the minute)

import java.util.Scanner;public class P1209 {public static void main(String[] args) {Scanner sc=new Scanner(System.in);int 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.