#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int arr1[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int i = 0;
int year = 0;
int month = 0;
int days = 0;
int week = 0;//判断第一天周几
scanf("%d%d", &year, &month);
if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))//判断是否是闰年
{
arr1[2] = 29;//闰年2月29天
}
for (i = 0; i < month; i++)
{
days += arr1[i];//计算该月第一天是该年多少天
}
days++;//该月第一天,还要把这一天算上
//套用计算星期几的公式
week = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + days) % 7;
printf(" 日 一 二 三 四 五 六\n");//表头
int arr2[50] = {0};
for (i = 0; i < week; i++)
{
printf(" ");//整型数组不能存放空格,因此直接把空格打印出来
}
arr2[week] = 1;
for (i = week + 1; i <= arr1[month]+week-1; i++)//把输入月份的每一天放到数组中
{
arr2[i] = arr2[i-1]+1;
}
int count = 0;//记录已打印几个数据,打印7个数据就换行
i = week;
int flag = 1;//用于记录是否为第一行,第一行不能用count计数方式换行
while (i<=arr1[month]+week-1)//把该月所有天数都打印出来
{
if (flag <= 7-week)
{
printf("%3d", arr2[i]);
if (flag == 7 - week)//打印够数据后换行
printf("\n");
flag++;
}
else
{
printf("%3d", arr2[i]);
count++;
if (count == 7)
{
count = 0;
printf("\n");
}
}
i++;
}
return 0;
}
实现结果: