Java经典算法题——猜生日

	/*
	 * 今年的植树节(2012年3月12日),小明和他的叔叔还有小伙伴们一起去植树。
	 * 休息的时候,小明的同学问他叔叔多大年纪,他叔叔说:“我说个题目,看你们谁先猜出来!”
	 * “把我出生的年月日连起来拼成一个8位数(月、日不足两位前补0)
	 * 正好可以被今天的年、月、日整除!”
	 * 他想了想,又补充到:“再给个提示,我是6月出生的。”
	 * 根据这些信息,请你帮小明算一下,他叔叔的出生年月日。
	 */

	public static void demo1() {
		for (int i = 20120312;; i--) {
			// String age = "" + i;
			String age = String.valueOf(i);
			int year = Integer.parseInt(age.substring(0, 4));
			int month = Integer.parseInt(age.substring(4, 6));
			int day = Integer.parseInt(age.substring(6, 8));
			if (!(i % 2012 == 0 && i % 03 == 0 && i % 12 == 0)) {
				continue;
			}
			if (day == 0 || day > 31) {
				continue;
			}
			if (month != 06) {
				continue;
			}
			System.out.println(i);
			break;

		}

	}

	public static void demo2() {

		int n = 20120312;
		String age = "";
		for (int i = 19000601; i <= 20130312; i++) {
			age = "" + i;
			int day = Integer.parseInt(age.substring(6)); // 得到天
			int month = Integer.parseInt(age.substring(4, 6)); // 得到月

			if (day >= 1 && day < 31 && month == 6 && i % 2012 == 0 && i % 3 == 0 && i % 12 == 0) {
				System.out.println(i);
			}
		}

	}

答案:19550604

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值