1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
using namespace std;
unsigned char zeroth[1+12+1] = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 17, 48, 78, 109 };
bool is_leap( int year )
{
return (bool)( !(year % 4) - !( year % 100) + !( year % 400 ) );
}
int days_in_month( int year, int month )
{
return ( month == 9 ) * 256 + zeroth[month+1] - zeroth[month]
+ is_leap( year ) * ( month == 2 );
}
int days_since_january_first( int year, int month, int day )
{
return zeroth[month] + ( month >= 10 ) * 256 + day - 1
+ is_leap( year ) * ( month > 2 ); // corrected
}
int main()
{
for ( int month = 1; month <= 12; month++ ) cout << month << " " << days_in_month( 2019, month ) << '\n';
for ( int month = 1; month <= 12; month++ ) cout << month << " " << days_in_month( 2020, month ) << '\n';
for ( int month = 1; month <= 12; month++ ) cout << month << " " << days_in_month( 1900, month ) << '\n';
for ( int month = 1; month <= 12; month++ ) cout << month << " " << days_in_month( 2000, month ) << '\n';
// for ( int year = 1859; year <= 2019; year++ ) cout << year << " " << is_leap( year ) << '\n';
// for ( int month = 1; month <= 12; month++ ) cout << month << " " << days_since_january_first( 2019, month, 1 ) << '\n';
// for ( int month = 1; month <= 12; month++ ) cout << month << " " << days_since_january_first( 2020, month, 1 ) << '\n';
// for ( int month = 1; month <= 12; month++ ) cout << month << " " << days_since_january_first( 2020, month, 29 ) << '\n';
}
|