如圖,給定任意時刻,求時針和分針的夾角(劣弧所對應的角)。php
輸入包含多組測試數據,每組測試數據由一個按hh:mm表示的時刻組成。java
對於每組測試數據,輸出一個浮點數,表明時針和分針的夾角(劣弧對應的角),用角度表示,結果保留兩位小數。測試
03:00 14:45
90.00 172.50
個人解答:spa
package jobdu.wangdao; import java.util.Scanner; /** * 給定任意時刻,求時針和分針的夾角(劣弧所對應的角)。 * */ public class Question1553 { /** * 計算時針位置 * * @param hour * @param minute * @return */ private static double houreAngle(int hour, int minute) { double angle = (hour % 12 / 12.0) * 360;// 本小時的角度 angle += (minute / 60.0) * (360 / 12);// 加上本小時走過的角度 return angle; } /** * 計算分針位置 * * @param minute * @return */ private static double minuteAngle(int minute) { double angle = (minute / 60.0) * 360; return angle; } /** * 計算圓周內兩個刻度之間的銳夾角 * * @param hourAngle * @param minuteAngle * @return */ private static double acuteAngle(double hourAngle, double minuteAngle) { double diff = Math.abs(hourAngle - minuteAngle); if (diff >= 180) diff = 360 - diff; return diff; } /** * 計算某個時刻時針分針夾角 * * @param time * @return */ public static double clockAngle(String time) { int hour = Integer.parseInt(time.split(":")[0]); int minute = Integer.parseInt(time.split(":")[1]); return acuteAngle(houreAngle(hour, minute), minuteAngle(minute)); } public static void main(String[] args) { @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String time = scanner.nextLine(); System.out.println(String.format("%.2f", clockAngle(time))); } } }