#include <stdio.h>

//利用泰勒公式计算星期几
//w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 
int getweek(int year,int month,int day)
{
	int w;    //星期
	int c;    //世纪-1 YYYY的头两位
	int y;    //年份   YYYY的后两位
	int m;    //月份 >=3 1月 2月看成上年的13月 14月
	int d=day;    //日
	if(month>=3) 
	{
		c=year / 100;
		y=year % 100;
		m=month;
	}
	else
	{
		m=month+12;
		y=(year-1) % 100;
		c=(year-1) / 100;
	}
	w=y+y/4+c/4-2*c+(26*(m+1))/10+d-1;
	w=(w+700) %7;
	return w;
}
//获取公历年某整月的天数
int get_day(int year,int month)
{
    int rui[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    int ping[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int ruiflag=0;
    if((year%4==0 &&year%100!=0) || year%400==0) ruiflag=1;
    if(ruiflag==1) return rui[month-1];else return ping[month-1];
}
int main()
{
	int i;
	int year;
	char* month[]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
	char* title[]={"周日","周一","周二","周三","周四","周五","周六"};
	printf("请输入年份:");
	scanf("%d",&year);      
	for(i=0;i<12;i++)
	{
		printf("%6s",month[i]);
		for(int j=0;j<7;j++) printf("%6s",title[j]);
		printf("\n      ");
		int flag=0;
		for(int k=1;k<=get_day(year,i+1);k++)
		{
			if(k==1) 
			{
				for(int j=0;j<6*getweek(year,i+1,k);j++) printf(" ");
				flag=getweek(year,i+1,k);
			}
			printf("%6d",k);
			if(k<7) 
			{
				if(flag+k==7) printf("\n      ");
			}
			else if((k+flag)%7==0) 	printf("\n      ");
		}
		printf("\n        =========================================\n");
	}
	return 0;
}