以2015年10月1日为开始 计算X天后为几年几月几日。X小于1500
答案:1 悬赏:70 手机版
解决时间 2021-11-14 11:42
- 提问者网友:暮烟疏雨之际
- 2021-11-14 02:31
以2015年10月1日为开始 计算X天后为几年几月几日。X小于1500
最佳答案
- 五星知识达人网友:鸠书
- 2021-11-14 02:58
#include
#define is_leap_year(year) (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
#define NONLEAP_YEAR_DAYS 365
static int nonleap_year_months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
typedef struct DateTime {
int year;
int month;
int day;
} DateTime;
int get_days_from_the_first_year(DateTime* today) {
int i;
int days = today->year * NONLEAP_YEAR_DAYS;
int leap_year_count = today->year / 4 + 1;
days += leap_year_count; // 闰年需要多加一天,一共有 leap_year_count 这么多闰年
// 接下来计算月和日
if (is_leap_year(today->year))
nonleap_year_months[1] = 29;
else
nonleap_year_months[1] = 28;
for (i = 0; i < today->month - 1; i++) {
days += nonleap_year_months[i];
}
days += today->day;
return days;
}
DateTime convert_to_date(int days) {
int period = 4 * NONLEAP_YEAR_DAYS + 1; // 4 年 1 闰,周期是 4 * 365 + 1
int period_count = days / period; // 周期个数
int remaining_years = 0;
DateTime dt;
// 多少个 4 年
dt.year = period_count * 4;
dt.month = 1;
dt.day = 1;
days -= period_count * period;
// 剩余的年数
remaining_years = (days - 1) / NONLEAP_YEAR_DAYS;
dt.year += remaining_years;
// 剩余天数
days = (days - 1 ) % NONLEAP_YEAR_DAYS; // 需要减掉闰年多出的一天。
// 统计月份和天数
{
int i;
if (remaining_years == 0) { // 刚好是头一年的闰年
nonleap_year_months[1] = 29;
}
else {
nonleap_year_months[1] = 28;
}
for (i = 0; i < 12; i++) {
days -= nonleap_year_months[i];
if (days > 0) {
dt.month++;
}
else {
dt.day = days + nonleap_year_months[i];
break;
}
}
}
return dt;
}
int main()
{
int days;
int total;
DateTime dt;
printf("Today is 2015.10.1
");
dt.year = 2015;
dt.month = 10;
dt.day = 1;
scanf("%d", &days);
total = get_days_from_the_first_year(&dt);
total += days;
dt = convert_to_date(total);
printf("After %d days, is %d.%d.%d
", days, dt.year, dt.month, dt.day);
return 0;
}
#define is_leap_year(year) (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
#define NONLEAP_YEAR_DAYS 365
static int nonleap_year_months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
typedef struct DateTime {
int year;
int month;
int day;
} DateTime;
int get_days_from_the_first_year(DateTime* today) {
int i;
int days = today->year * NONLEAP_YEAR_DAYS;
int leap_year_count = today->year / 4 + 1;
days += leap_year_count; // 闰年需要多加一天,一共有 leap_year_count 这么多闰年
// 接下来计算月和日
if (is_leap_year(today->year))
nonleap_year_months[1] = 29;
else
nonleap_year_months[1] = 28;
for (i = 0; i < today->month - 1; i++) {
days += nonleap_year_months[i];
}
days += today->day;
return days;
}
DateTime convert_to_date(int days) {
int period = 4 * NONLEAP_YEAR_DAYS + 1; // 4 年 1 闰,周期是 4 * 365 + 1
int period_count = days / period; // 周期个数
int remaining_years = 0;
DateTime dt;
// 多少个 4 年
dt.year = period_count * 4;
dt.month = 1;
dt.day = 1;
days -= period_count * period;
// 剩余的年数
remaining_years = (days - 1) / NONLEAP_YEAR_DAYS;
dt.year += remaining_years;
// 剩余天数
days = (days - 1 ) % NONLEAP_YEAR_DAYS; // 需要减掉闰年多出的一天。
// 统计月份和天数
{
int i;
if (remaining_years == 0) { // 刚好是头一年的闰年
nonleap_year_months[1] = 29;
}
else {
nonleap_year_months[1] = 28;
}
for (i = 0; i < 12; i++) {
days -= nonleap_year_months[i];
if (days > 0) {
dt.month++;
}
else {
dt.day = days + nonleap_year_months[i];
break;
}
}
}
return dt;
}
int main()
{
int days;
int total;
DateTime dt;
printf("Today is 2015.10.1
");
dt.year = 2015;
dt.month = 10;
dt.day = 1;
scanf("%d", &days);
total = get_days_from_the_first_year(&dt);
total += days;
dt = convert_to_date(total);
printf("After %d days, is %d.%d.%d
", days, dt.year, dt.month, dt.day);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯