星际炸弹
题目
//在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
//
//每个炸弹都可以设定多少天之后爆炸。
//
//比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
//
//有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
//
//
//
//请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19
//
//请严格按照格式书写。不能出现其它文字或符号。
思路
两个种解决办法。1、调用calendar API 2、模拟日期加减。 这里选择用模拟日期加减。因为api没有挑战难度。
主要是思考,日期超过了30或31 时的处理 以及 超过12月份的处理方式。数组0位不用,当偏置,方便写代码
代码
public class 星际炸弹 {
static int [] month= {0,31,30,31,30,31,30,31,31,30,31,30,31};
public static void main(String[] args) {
int tmpYear=2014;int tmpMonth=11;int tmpday=9;
isleap(tmpYear);
for(int i =1;i<=1000;i++) {
tmpday++;
if(tmpday>month[tmpMonth]) {
tmpday=1;
tmpMonth++;
if(tmpMonth>12) {
tmpMonth=1;
tmpYear++;
isleap(tmpYear);
}
}
}
System.out.println(tmpYear+"-"+tmpMonth+"-"+tmpday);
}
public static void isleap(int year) {
if(year%4==0&&year%100!=0||year%400==0)month[2]=29;
else month[2]=28;
}
}