题解 第二章暴力求解| #日期类#
日期类
http://www.nowcoder.com/practice/130aa2d7d1f5436b920229dca253893b
思路非常简单
即设计一个保存日期的类
在这个类中添加日期自增1和打印日期的功能即可
#include <iostream>
using namespace std;
static const int month[2][13]=
{
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
static const int year[2]={365,366};
class Date
{
public:
Date()=default;
Date(int YY,int MM,int DD):Y(YY),M(MM),D(DD) {}
Date(const Date &date):Y(date.Y),M(date.M),D(date.D) {}
~Date(){}
void setDate(int YY,int MM,int DD) {Y=YY;M=MM;D=DD;}
void datePrint(){printf("%04d-%02d-%02d\n",Y,M,D);}
//void datePrint(){printf("%4d-%2d-%2d\n",Y,M,D);}
//如果这样写,则输出不会被0填充,而是被空格填充
void PlusOneDay();
private:
int Y=0,M=0,D=0;
};
void Date::PlusOneDay()
{
int isRun=0;
if(!(Y%4)&&Y%100||Y%400)
isRun=1;
if(D==month[isRun][M])
{
D=1,M++;
if(M==13)
M=1,Y++;
}
else D++;
}
int main()
{
Date inputDay;
int inputY,inputM,inputD;
int repeatTimes=0;
cin>>repeatTimes;
while(repeatTimes--)
{
cin>>inputY>>inputM>>inputD;
inputDay.setDate(inputY,inputM,inputD);
inputDay.PlusOneDay();
inputDay.datePrint();
}
return 0;
}
王道机试指南刷题 文章被收录于专栏
计划刷完这本书