229
328
1479
金牌会员
使用道具 举报 送花
10
1161
5133
论坛元老
#include <stdio.h> typedef unsigned char u8; typedef unsigned int u16; typedef int s16; u8 is_leap_year(u16 year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } u16 days_in_month(u16 year, u8 month) { static const u8 days_in_months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2 && is_leap_year(year)) { return 29; } return days_in_months[month - 1]; } u16 days_from_epoch(u16 year, u8 month, u8 day) { u16 days = day; u16 y; for (y = 0; y < year; ++y) { days += is_leap_year(y) ? 366 : 365; } for (u8 m = 1; m < month; ++m) { days += days_in_month(year, m); } return days; } s16 days_between(u16 year1, u8 month1, u8 day1, u16 year2, u8 month2, u8 day2) { return days_from_epoch(year2, month2, day2) - days_from_epoch(year1, month1, day1); } void main() { u16 year, current_year; u8 month, day, current_month, current_day; // 输入指定日期 printf("输入指定日期 (年 月 日): "); scanf("%u %hhu %hhu", &year, &month, &day); // 获取当前日期(模拟) current_year = 2024; current_month = 6; current_day = 27; // 计算天数差 s16 diff = days_between(current_year, current_month, current_day, year, month, day); printf("指定日期与今天相差 %d 天\n", diff); } 复制代码
本版积分规则 发表回复 回帖后跳转到最后一页
|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )
GMT+8, 2025-5-4 04:06 , Processed in 0.134758 second(s), 51 queries .
Powered by Discuz! X3.5
© 2001-2025 Discuz! Team.