zoukankan      html  css  js  c++  java
  • hdu1209(Clock)

    点击打开hdu1209

    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.
     

    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

    思路:这题主要是求时钟和分钟夹角的大小。然后进行排序。并且还要注意一个小细节问题。当夹角相等时,时间小的放在前面。

    夹角求法:分钟旋转一周要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(){
    		
    	}
    }
    


  • 相关阅读:
    STL源代码剖析(二)
    局域网部署docker--从无到有创建自己的docker私有仓库
    Leetcode Add two numbers
    GDIPlus绘制桌面歌词
    Android中apk动态载入技术研究(2)android插件化及实现
    jq 地区(省市县区)联动菜单
    System.Diagnostics.Process.Start的妙用
    aaaa
    RESTful Web 服务:教程
    芒果TV 视频真实的地址获取
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7096175.html
Copyright © 2011-2022 走看看