用C++类的构造函数自动完成
#include<iostream> using namespace std; class Date{ public: Date(int year,int day) :_day(0),_month(1),_year(year) { int tmp=day; isleapyear=(!(_year % 4) && _year % 100) || !(_year % 400); for(int mday=getmonthday(_month);tmp>mday;mday=getmonthday(_month)){ _month++; if(_month==13){ _year++; _month=1; } tmp-=mday; } _day=tmp; printf("%04d-%02d-%02d\n", _year, _month, _day); } private: int getmonthday(int month){ static int monthdayarr[]={31,28,31,30,31,30,31,31,30,31,30,31}; return monthdayarr[month-1]+(month==2&&isleapyear?1:0); } bool isleapyear; int _year; int _month; int _day; }; int main(){ int year,day; while(~scanf("%d %d",&year,&day)){ Date tmp(year,day); } }看着代码多 其实3ms 超过73%