Run ID:2611

提交时间:2024-01-15 22:22:51

#include<iostream> using namespace std; struct Date { int year, month, day; }; bool isLeapYear(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } int daysInMonth(int year, int month) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: return (isLeapYear(year) ? 29 : 28); default: return -1; // Invalid month } } int daysBetweenDates(const Date& start, const Date& end) { int days = 0; while (start.year < end.year || start.month < end.month || start.day < end.day) { int daysInCurrentMonth = daysInMonth(start.year, start.month); while (start.day < daysInCurrentMonth) { start.day++; days++; } if (start.month == 12 && start.day == daysInCurrentMonth) { start.year++; start.month = 1; start.day = 1; } else if (start.day == daysInCurrentMonth) { start.month++; start.day = 1; } } return days; } int main() { int t; cin >> t; while (t--) { Date birthDate, age18Date; char sep; cin >> birthDate.year >> sep >> birthDate.month >> sep >> birthDate.day; age18Date.year = birthDate.year + 18; age18Date.month = birthDate.month; age18Date.day = birthDate.day; if (age18Date.month == 2 && age18Date.day == 29 && !isLeapYear(age18Date.year)) { cout << -1 << endl; } else { int days = daysBetweenDates(birthDate, age18Date); cout << days << endl; } } return 0; }