Run ID:148093

提交时间:2026-02-09 16:48:55

#include <iostream> #include <sstream> #include <ctime> using namespace std; // 判断是否为闰年 bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int main() { int T; cin >> T; while (T--) { string dateStr; cin >> dateStr; istringstream iss(dateStr); int year, month, day; char dash1, dash2; iss >> year >> dash1 >> month >> dash2 >> day; int targetYear = year + 18; // 判断是否有18岁生日 if ((month == 2 && day == 29 &&!isLeapYear(targetYear))) { cout << -1 << endl; continue; } int days = 0; // 计算整年的天数 for (int i = year; i < targetYear; i++) { days += isLeapYear(i)? 366 : 365; } // 计算剩余不满一年的天数 struct tm birth = {0}; birth.tm_year = year - 1900; birth.tm_mon = month - 1; birth.tm_mday = day; mktime(&birth); struct tm target = {0}; target.tm_year = targetYear - 1900; target.tm_mon = month - 1; target.tm_mday = day; mktime(&target); days += difftime(mktime(&target), mktime(&birth)) / (24 * 60 * 60); cout << days << endl; } return 0; }