Advertisement
Go-Ice

UVa [579 - Clock Hands] by Go-Ice

Oct 9th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. /**
  2.  * Date: 2014.10.09
  3.  * @author LinChuWen
  4.  * UVa Online Judge Problem #579 - Clock Hands
  5.  * Description: The output displays the smallest positive angle in degrees between the hands for each time.
  6.  *      The answer should between 0 degrees and 180 degrees for all input times.
  7.  *      Display each angle on a line by itself in the same order as the input.
  8.  *      The output should be rounded to the nearest 1/1000.
  9.  */
  10. import java.util.*;
  11. public class UVa_579 {
  12.    
  13.     public static void main(String[] args) {
  14.         Scanner input = new Scanner(System.in);
  15.         String time,str_h,str_m;
  16.         int h,m;
  17.         int length;
  18.         float dh,dm,answer;
  19.        
  20.         while(input.hasNext()){
  21.             time = input.nextLine();
  22.             length = time.length();
  23.             str_m = time.substring(length-2, length);
  24.             str_h = time.substring(0, length-3);
  25.             m = Integer.parseInt(str_m);
  26.             h = Integer.parseInt(str_h);
  27.            
  28.             if(h==0 && m==0)
  29.                 break;
  30.                
  31.             dh=(float) (h*30+m*0.5);
  32.             dm=(float) (m*6.0);
  33.            
  34.             answer=dh-dm;
  35.             if(answer<0)
  36.                 answer=-answer;
  37.             if(answer>180)
  38.                 answer=360-answer;
  39.            
  40.             System.out.printf("%.3f\n",answer);
  41.         } //big while end
  42.         input.close();
  43.     } //main end
  44. } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement