Run ID:148098
提交时间:2026-02-09 16:50:24
#include <iostream> using namespace std; bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int getDaysInYear(int year) { return isLeapYear(year) ? 366 : 365; } bool isValidDate(int year, int month, int day) { if (month < 1 || month > 12) return false; if (day < 1) return false; int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (isLeapYear(year) && month == 2) { return day <= 29; } return day <= daysInMonth[month - 1]; } int main() { int T; cin >> T; for (int i = 0; i < T; i++) { int year, month, day; scanf("%d-%d-%d", &year, &month, &day); // 检查出生日期有效性 if (!isValidDate(year, month, day)) { cout << "-1" << endl; continue; } int targetYear = year + 18; // 特殊情况:2月29日出生的人在非闰年没有18岁生日 if (month == 2 && day == 29 && !isLeapYear(targetYear)) { cout << "-1" << endl; continue; } // 计算18年后的日期是否有效 if (!isValidDate(targetYear, month, day)) { cout << "-1" << endl; continue; } // 计算总天数 int totalDays = 0; for (int y = year; y < targetYear; y++) { totalDays += getDaysInYear(y); } cout << totalDays << endl; } return 0; }