20
社区成员




在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标。
每个炸弹都可以设定多少天之后爆炸。
比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸。
有一个贝塔炸弹,2014年11月9日放置,定时为1000天,请你计算它爆炸的准确日期。
以下程序实现了这一功能,请你填补空白处内容:
提示:
json
先判断是否为闰年,这会影响2月份是28还是29,如果是闰年,2月份是29,如果不是,就是28
#include <stdio.h>
int main()
{
int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int days = 1000;
int year = 2014, month = 11, day = 9;
int i;
for (i = 0; i < days; i++)
{
day++;
if (day > monthDays[month - 1])
{
day = 1;
month++;
if (month > 12)
{
month = 1;
year++;
____________________;
}
}
}
printf("%d-%d-%d\n", year, month, day);
getchar();
return 0;
}
太简单了,只看了选项就能答出来
#include"stdio.h"
int main(void){
int months[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int year = 2014,month = 11,day = 9;
for(int i = 0;i < 1000;i++){
day++;
if(day > months[month]){
day = 1;
month++;
}if(month > 12){
month = 1;
year++;
}
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)){
months[2] = 29;
}else months[2] = 28;
}
printf("%d.%d.%d\n",year,month,day); // 2017.8.5
return 0;
}
刷题,评论即为笔记。这题比较友好关键是判断闰年平年,即使你不知道如何判断也应该知道闰年的二月多一天为29天,从选项来看只需要找到29 ,28 即可
四年一闰;百年不闰,四百年再闰
public static void main(String[] args) throws ParseException {
function("2014-11-9",1000);
}
public static void function(String date,long sum) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//计算date的毫秒值
long time = dateFormat.parse(date).getTime();
//计算sum的毫秒值
sum = sum*24*60*60*1000;
//以date+sum的毫秒值创建Date
Date dateTime = new Date((time + sum));
//Date转String
String s = dateFormat.format(dateTime);
System.out.println(s);
}
妥妥的细节,当我考虑闰年的判断条件时,他考的是闰年有28天还是29天,还是30天,又或者31天???
感觉是没必要计算闰年啊,时间戳计算不就可以了。我的解题(js实现)
https://blog.csdn.net/xuelang532777032/article/details/124318063#5_155
28,29
有没有高手给讲解一下 星系炸弹 这道题?