在一天的24小时之中,时钟的时针、分针和秒针完全重合在一起的时候有几次?
我们做几个假设:
1.一个时钟有60个刻度;
2.秒针从0移动到60,分针移动一个刻度;
3.分针从0开始每移动12个刻度,时针则移动1个刻度.
结果是24个重合的时刻.
以下是java代码:
public static void main(String args[]) {
final int n = 3600 * 24;int i1 = 0, i2 = 0, i3 = 0;// 分别代表时针,分针,和秒针指向的刻度.int x = 0;int sum = 0;for (int i = 0; i < n; i++) {i3 = i % 60;// 秒针指向的刻度i2 = (i / 60) % 60;// 走过的分钟数除60的余数.i1 = (i / 3600) * 5 + i2 / 12;// 小时数*5+分钟数/12if (((i1 < 60 ? i1 : i1 % 60) == i2) && (i2 == i3)) {System.out.println(String.format("%02d", (i1 / 5)) + ":"+ String.format("%02d", i2) + ":"+ String.format("%02d", i3) + ", "+ String.format("%05d", i) + ", "+ String.format("%04d", (i - x)));x = i;sum++;}}System.out.println(sum);}
结果:
00:00:00, 00000, 0000
01:05:05, 03905, 3905
02:10:10, 07810, 3905
03:16:16, 11776, 3966
04:21:21, 15681, 3905
05:27:27, 19647, 3966
06:32:32, 23552, 3905
07:38:38, 27518, 3966
08:43:43, 31423, 3905
09:49:49, 35389, 3966
10:54:54, 39294, 3905
11:59:59, 43199, 3905
12:00:00, 43200, 0001
13:05:05, 47105, 3905
14:10:10, 51010, 3905
15:16:16, 54976, 3966
16:21:21, 58881, 3905
17:27:27, 62847, 3966
18:32:32, 66752, 3905
19:38:38, 70718, 3966
20:43:43, 74623, 3905
21:49:49, 78589, 3966
22:54:54, 82494, 3905
23:59:59, 86399, 3905
24
No comments:
Post a Comment