酷代码 AI
菜单
服务商

C++ 实现:从计算机极客小蓝的整数日期推算真实日期

#include <iostream> using namespace std; // 判断闰年 bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; } // 计算日期 void calculateDate(int days) { int year = 1999; int monthDays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; while (days > 365) { int daysInYear = isLeapYear(year) ? 366 : 365; if (days > daysInYear) { days -= daysInYear; year++; } } if (isLeapYear(year)) { monthDays[2] = 29; } int month = 1; while (days > monthDays[month]) { days -= monthDays[month]; month++; } cout << year << "-"; if (month < 10) { cout << "0" << month << "-"; } else { cout << month << "-"; } if (days < 10) { cout << "0" << days << endl; } else { cout << days << endl; } } int main() { int days[] = {7856}; // 这里可以添加其他整数 for (int i = 0; i < 1; i++) { cout << "整数 " << days[i] << " 对应的日期为:"; calculateDate(days[i]); } return 0; } ######[AI写代码神器 | 761点数解答 | 2024-11-28 19:22:00]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]