星際炸彈
題目
//在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;
}
}