酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

小蓝是一名计算机极客,他在记录一些重要的日子时从不注明年月日,而是用一个整数替代,比如 4532,后来人们知道,那个整数就是日期,这个整数表示的日期就是他出生后的第几天。 他出生于:1999−04−30 例如他的日记里记录着获得蓝桥杯国寒总冠军的日子为 7856 这个整数,可以推断出这一天是 2020−10−31,现在需要请你计算出小蓝日记中其他整数对应的日期。 注意:输出的日期格式:yyyy-mm-dd,如:2020−03−21(月和日小于 10 的需要在月和日前补 0 )。 用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; } [2024-11-28 19:22:00 | AI写代码神器 | 761点数解答]

相关提问