Hang electric OJ 1006 title link: Click to open the link

The idea: The first attempt to use the enumeration method, in S-unit traversal found that the results are not accurate, in MS units can meet the precision requirements, but will time out. The final problem solving ideas are as follows:

(1) the second hand 1s walk 6 degrees, the minute hand 1s go 0.1 degrees, clockwise 1s walk 1/120 degrees. So the minute hand second seconds of the degree difference is 5.9°, the hour hand hand minute difference is 11/120°, the hour hand hand seconds of the degree difference is 719/120°.

(2) Starting from 00:00:00 to consider, at this point the minute needle points to the top 12 point direction. 12 hours later, the second and second pins point again to the top, complete a cycle, so only need to calculate the 12 hours of three pointer happy cumulative "seconds", and then divided by 12 hours of total seconds to get the result (12 hours and 24 hours of the result is the same ~).

(3) Starting from 00:00:00 to consider, according to the above conditions, you can calculate the hour hand hand, hand hand, minute hand seconds in 12 hours, each time meet the happy conditions of the start and end time, using the array to save.

(4) It should be noted that in order to satisfy the happy condition, it is necessary to judge whether the clockwise angle 1 satisfies the condition, and also to determine whether the Dianding 2 of the angle 1 satisfies the condition. Because the angle 1 and the angle 2 are the angles that these two pointers constitute.

(5) using a number axis to represent time (it is easier to draw a picture using a number axis), to find the intersection based on the values saved in the array, you can locate the time period when the minute hand hand is happy.

The AC Reference code:

Import Java.io.BufferedInputStream;
Import Java.text.DecimalFormat;
Import Java.util.Scanner; public class Main {private static Double Max (double A, double b, double c) {A = a > b a:b;//A The value is modified to the larger of the A,b A = a > c? A:C;
The value of a is modified to the larger of the a,c (the calculated a is already the larger number in the A,b, so the final A is the largest in a,b,c) return a;
private static Double min (double A, double b, double c) {A = a < b a:b; A = a < C?
A:C;
return A;
public static void Main (string[] args) {Scanner cin = new Scanner (new Bufferedinputstream (system.in));
int total = 12 * 60 * 60; Record the starting/ending time between the 22 pointers and the array that records the minute hand happy the start and end times. b in the BMS indicates that e in Begin,ems represents end//array size 708, it is because the minute hand is coincident 708 times in 12 hours, so the starting and ending times of the happy is also 708 times//(coincident 708 times is calculated in advance)//To prevent array overflow, the array size is set to 70
9 double[] BMS = new double[709];
double[] ems = new double[709];
An array used to record the hours of the hour hand happy.
12 hours of hand hands coincident 11 times double[] BHM = new DOUBLE[12];
double[] Ehm = new DOUBLE[12];
An array that records the happy and ending times of the hour hand. The minute hand hand 12 hours overlap 719 times double[] BHS = new DoubLE[720];
double[] EHS = new double[720];
The degree of difference per second of minute hand seconds double dms = 5.9;
The degree difference per second of the hour hand minute is double dhm = 11.0/120;
Double DHS = 719.0/120, with degrees per second.
Double d = cin.nextdouble ();
while (d!=-1) {//12 hours, Happy time difference cumulative double count = 0; Calculate and populate the values of each array bms[0] = D/DMS; Minute hand seconds the first happy start time (clockwise angle, start satisfies the condition) ems[0] = (360.0-d)/DMS; Minute hand seconds the end time of the first happy (the Dianding of the clockwise angle, starting to not meet the condition) for (int i = 1; i < 709; i++) {//minute hand seconds coincident, the start time of the happy condition is met Bms[i] = (360.0 * i)/dms + bms[0];
(360*i)/dms: Time ems[i] = (360.0 * i)/dms + ems[0) for the minute hand second hand;
///Calculate BHM, Ehm, BHS, EHS Truth and the same bhm[0] = D/DHM;
Ehm[0] = (360.0-d)/DHM;
for (int i = 1; i < i++) {Bhm[i] = (360.0 * i)/DHM + bhm[0];
Ehm[i] = (360.0 * i)/DHM + ehm[0];
} Bhs[0] = D/DHS;
Ehs[0] = (360.0-d)/DHS;
for (int i = 1; i < 720 i++) {bhs[i] = (360.0 * i)/DHS + bhs[0];
Ehs[i] = (360.0 * i)/DHS + ehs[0];
} I, J, K are respectively used to record Bhs[i], bms[j], Bhm[k] array of the current access position int i = 0;
int j = 0;
int k = 0;
The time and the seconds the needle satisfies the happy condition start and end time double begin = 0.0;
Double end = 0.0;
while (beginning <= total && end <= total) {//Start time takes a larger value begin = Max (Bhs[i], bms[j], bhm[k]);
End time to take a smaller value ends = min (Ehs[i], ems[j], ehm[k]);
In one cycle, only one of the values in I, J, K will be changed, and the relative last one will move forward ...
Not too good to explain, drawing axis will better understand if (end = = Ehs[i]) {i++;
} if (end = = Ems[j]) {j + +;
} if (end = = Ehm[k]) {k++;
} if (end > Begin && End <= total) {count + = (end-begin);
}///leave three-digit decimal output DecimalFormat formatter = new DecimalFormat ("#0.000");
Double result = ((Count + 0.00)/total) * 100;
System.out.println (Formatter.format (result));
D = cin.nextdouble ();
} cin.close ();
}
}

Welcome to comment on the discussion, do not like to spray.