题解 | #日期类#
日期类
https://www.nowcoder.com/practice/130aa2d7d1f5436b920229dca253893b
#include <iostream> #include <cstdio> #include <iomanip> using namespace std; int daytable[2][13]={ {31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31} }; bool IsLeapYear(int year){ //判断是否是闰年 return (year%4==0 && year%100!=0) || (year%400==0); //如果是闰年返回1,否则返回0 } int main(){ int n; int year,month,day; cin>>n; while(n--){ cin>>year>>month>>day; int row=IsLeapYear(year); if(day<daytable[row][month-1]){ cout<<setw(4)<<setfill('0')<<year<<'-'<<setw(2)<<setfill('0')<<month<<'-'<<setw(2)<<setfill('0')<<day+1<<endl; }else if(month<11){ cout<<setw(4)<<setfill('0')<<year<<'-'<<setw(2)<<setfill('0')<<month+1<<'-'<<setw(2)<<setfill('0')<<'1'<<endl; }else{ cout<<setw(4)<<setfill('0')<<year+1<<'-'<<setw(2)<<setfill('0')<<'1'<<'-'<<setw(2)<<setfill('0')<<'1'<<endl; } } return 0; }
超月、月+1;超年、年+1;未超日+1;额外考虑闰年情况就行。