AC program inside. In fact, there can be trailing spaces before end of lines.
#include <iostream>
using namespace std;
inline bool is_leap_year(int y)
{
return y%4==0&&(y%100!=0||y%400==0);
}
inline int days_per_year(int y)
{
if(is_leap_year(y))
return 366;
else
return 365;
}
inline int days_per_month(int m,int y)
{
const int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(m!=2)
return days[m-1];
else if(is_leap_year(y))
return 29;
else
return 28;
}
int get_day_of_week(int m,int d,int y)
{
int day=4;
int i;
for(i=1600;i<y;i++)
day+=days_per_year(i);
for(i=1;i<m;i++)
day+=days_per_month(i,y);
day+=d;
return day%7;
}
void print_calendar(int m,int d,int y)
{
const char* day_name[7]=
{"mon","tue","wed","thu","fri","sat","sun"};
int days=days_per_month(m,y);
int day_begin=1-get_day_of_week(m,1,y);
for(int i=0;i<7;i++)
{
cout<<day_name[i];
for(int j=day_begin+i;j<=days;j+=7)
if(j<1||j>days)
cout<<" ";
else if(j==d)
{
if(j<10)
cout<<" [ "<<j<<"]";
else
cout<<" ["<<j<<"]";
}
else
{
if(j<10)
cout<<" "<<j<<" ";
else
cout<<" "<<j<<" ";
}
cout<<endl;
}
}
int main()
{
int d,m,y;
cin>>d>>m>>y;
print_calendar(m,d,y);
return 0;
}