#include #include using namespace std;

bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); }

int daysOfMonth(int year, int month) { int days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; if (month == 2 && isLeapYear(year)) { return 29; } return days[month - 1]; }

string subtractOneDay(string date) { int year = stoi(date.substr(0, 4)); int month = stoi(date.substr(5, 2)); int day = stoi(date.substr(8, 2));

if (day > 1) {
    day--;
} else {
    if (month > 1) {
        month--;
        day = daysOfMonth(year, month);
    } else {
        year--;
        month = 12;
        day = 31;
    }
}

string result = to_string(year) + "-";
if (month < 10) {
    result += "0";
}
result += to_string(month) + "-";
if (day < 10) {
    result += "0";
}
result += to_string(day);

return result;

}

int main() { int T; cin >> T;

for (int i = 0; i < T; i++) {
    string date;
    cin >> date;
    
    cout << subtractOneDay(date) << endl;
}

return 0;

}


原文地址: https://www.cveoy.top/t/topic/pI7F 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录