Run ID:148240

提交时间:2026-02-10 11:39:52

#include <iostream> #include <string> #include <vector> using namespace std; // 判断是否为闰年 bool isLeapYear(int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } // 获取某年某月的天数 int getDaysInMonth(int year, int month) { if (month == 2) { return isLeapYear(year) ? 29 : 28; } if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } return 31; } // 计算从出生到18岁生日的天数 int calculateDays(int birthYear, int birthMonth, int birthDay) { // 检查是否有18岁生日(出生在闰年2月29日,且18岁生日那年不是闰年) if (birthMonth == 2 && birthDay == 29 && !isLeapYear(birthYear + 18)) { return -1; } int totalDays = 0; // 计算出生那年的剩余天数 int year = birthYear; for (int month = birthMonth; month <= 12; month++) { int daysInMonth = getDaysInMonth(year, month); int startDay = (month == birthMonth) ? birthDay : 1; totalDays += (daysInMonth - startDay + 1); } // 计算中间完整年份的天数(出生后第一年到第17年) for (int y = birthYear + 1; y < birthYear + 18; y++) { totalDays += (isLeapYear(y) ? 366 : 365); } // 计算18岁生日那年前几个月的天数 year = birthYear + 18; for (int month = 1; month < birthMonth; month++) { totalDays += getDaysInMonth(year, month); } // 加上18岁生日那月生日前的天数(不包括生日当天) totalDays += (birthDay - 1); return totalDays; } int main() { int T; cin >> T; while (T--) { string date; cin >> date; // 解析日期字符串 int birthYear = stoi(date.substr(0, 4)); int birthMonth = stoi(date.substr(5, 2)); int birthDay = stoi(date.substr(8, 2)); cout << calculateDays(birthYear, birthMonth, birthDay) << endl; } return 0; }