hdu1209(Clock),hdu1209clock

來源:互聯網
上載者:User

hdu1209(Clock),hdu1209clock

點擊開啟hdu1209

Problem DescriptionThere 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.
 
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

思路:這題主要是求時鐘和分鐘夾角的大小,然後進行排序,而且還要注意一個小細節問題,當夾角相等時,時間小的放在前面。

夾角求法:分鐘旋轉一周要60分鐘,所以分鐘每分鐘旋轉360度除以60,為6度,而時鐘轉一周要12小時,一小時等於60分鐘,所以分鐘轉動的速度是時鐘轉動的12倍,即時鐘轉動速度為0,5度每分鐘。

從而得等公式r=h*30+0.5*m-m*6(h*30:代表時鐘準點的度數,而0.5*m:表示轉動m分鐘時,時鐘轉動的度數,二者相加即為時鐘的總的轉的角度,m*6則是分鐘轉動的角度)

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(){}}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.