#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;
7-138 昨天分数 10作者 黄龙军单位 绍兴文理学院小明喜欢上了日期的计算。这次他要做的是日期的减1天操作即求在输入日期的基础上减去1天后的结果日期。例如:日期为2019-10-01减去1天则结果日期为2019-09-30。输入格式首先输入一个正整数T表示测试数据的组数然后是T组测试数据。每组测试输入1个日期日期形式为yyyy-mm-dd。保证输入的日期合法而且输入的日期和计算结果都在1000

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

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