星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
请填写该日期,格式为 yyyy-mm-dd 即4位年份2位月份2位日期。比如:2015-02-19
请严格按照格式书写。不能出现其它文字或符号。
#include<iostream>
using namespace std;
int leap(int n)
{
if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0)
return 1;//若是闰年返回1;
else
return 0;
}
int main()
{
int year,month,day;
int leapyear[2] = { 365,366 };
int months[][12] = { 31,28,31,30,31,30,31,31,30,31,30,31,
31,29,31,30,31,30,31,31,30,31,30,31 };
int n;//n表示输入的天数;
while (cin >> n)
{
n = n + 313;//从2014年开始,将初始的月份日期转换成天数;
for (year = 2014;n >= leapyear[leap(year)];year++)
{
n -= leapyear[leap(year)];
}
for (month = 0;n >= months[leap(year)][month];month++)
{
n -= months[leap(year)][month];
}
day = n ;
cout << year << "-";
if (month < 9)
cout << "0" << month + 1;
else
cout << month+1;
cout << "-";
if (day < 10)
cout << "0" << day << endl;
else
cout << day << endl;
}
return 0;
}