时钟指针重合问题java实现

先把结果贴上去吧,写这个程序的目的也就是想看看到底是那些值:

1:5:27 2:10:54 3:16:21 4:21:49 5:27:16 6:32:43 7:38:10 8:43:38 9:49:5 10:54:32 12:0:0 13:5:27 14:10:54 15:16:21 16:21:49 17:27:16 18:32:43 19:38:10 20:43:38 21:49:5 22:54:32 24:0:0 根据记后不记前的理论来说一天有22次。算法是很简单的,但是没写过的话当面试什么的当场用手推还是不容易的,还有求时针,分针,秒针重合的问题,简单点可以直接在上面的值分别求三个针的角度看相等的有那些,笔算的话就是解三元方程了,这个不管笔算还是编程都要复杂多了。

package test;
/**
 * 
 * 经典的时钟指针重合问题
 *
 */
public class timecover {
	
	//该方法是求时针和分针重合时具体的时间(精确到秒)
	protected void timehm() {
		//这里min要赋一个值,不然下面无法运算
		double min = 0;
		double x;
		String str = new String();
		//2πx = 2π + π/6x 方程原理等同于距离 = 时间 * 速度
		x = 720.0/11.0; 
		for (double i = 0.0; i < 1440.0/x; i++) {
			min += x; 
			str += (int)(min*60) + "\t";
		}
		//对象才能使用方法,变量不能
		String result = mtimeup(str);
		System.out.println(result);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
        timecover a1 = new timecover();
        a1.timehm();
	}
	
	public static String mtimeup(String str) {
		String[] arry = str.split("\t");
		String result = new String();
		for (int i = 0; i < arry.length; i++) {
			int x = Integer.parseInt(arry[i]) ;
			result += Integer.toString(x/3600) 
			+ ":" + Integer.toString((x-(x/3600*3600))/60)
			+ ":" + Integer.toString((x-(x/3600*3600))%60)+ "\t" ;
		}
		return result;	
	}
}


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值