题解 | #日期类#
日期类
https://www.nowcoder.com/practice/130aa2d7d1f5436b920229dca253893b
c++格式输出
指定位数:setw(位数值)
补零输出:setfill('0')
#include <iomanip> #include <iostream> using namespace std; int mon[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int m; while (cin >> m) { // 注意 while 处理多个 case for (int i = 0; i < m; i++){ int year, month, day; cin >> year >> month >> day; //是否是闰年 int run = 0; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) run = 1; //加一天 if (day <= mon[month-1] || month == 2 && day == 29){ if (run && month == 2 && day == 28){ //是闰年 则day=29 day += 1; } else if (day == mon[month-1]){ //当月最后一天 month += 1; day = 1; } else{ day += 1; } } //处理当年的最后一天的情况 year += month / 12; month %= 12; //printf("%d-%02d-%02d\n",year,month,day); cout << year <<"-"<<setw(2)<<setfill('0')<< month <<"-"<<setw(2)<<setfill('0')<< day << endl; } } } // 64 位输出请用 printf("%lld")