日期类问题大总结
- 打印日期Acwing 3607
- 日期累加Acwing 3573
- 日期计算Acwing 3218
3 . 日期计算
#include<iostream>
using namespace std;
const int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int is_leap_year(int year){
if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) return 1;
return 0;
}
int getmonthdays(int year , int month){
if(is_leap_year(year) && month == 2) return 29;
return months[month];
}
int main()
{
int y , t;
cin >> y >> t;
int res1 = t;
int d = 1 , m = 1;
t --;
while(t -- ){
if(++d > getmonthdays(y ,m)){
d = 1;
if( ++ m > 12){
y ++;
m = 1;
}
}
}
for(int i = 1; i < m; i ++) res1 -= getmonthdays(y,i) ;
cout << m << endl ;
cout << res1 << endl;
return 0;
}