// 包含兩種I/O庫,可以使用任一種輸入輸出方式
include <stdio.h>
include
using namespace std;
// 函數printMonth:按要求的格式打印某年某月的日歷
// 參數:year-年,month-月
// 返回值:無
void printMonth(int year, int month);
// leapYear:判斷閏年
// 參數:y-年
// 返回值:1-是閏年,0-不是閏年
int leapYear(int y)
{
if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
return 1;
return 0;
}
// 函數whatDay:計算某年某月的1號是星期幾
// 參數:year-年,month-月
// 返回值:1到7--星期1到星期日
int whatDay(int year, int month)
{
// 1年月日是星期一
int w = 1;
int i;
// 1到year-1都是全年
for(i = 1; i < year; i++)
{
if(leapYear(i))
w += 366;
else
w += 365;
}
switch(month)
{
case 12: // 加月的
w += 30;
case 11: // 加月的
w += 31;
case 10: // 加月的
w += 30;
case 9: // 加月的
w += 31;
case 8: // 加月的
w += 31;
case 7: // 加月的
w += 30;
case 6: // 加月的
w += 31;
case 5: // 加月的
w += 30;
case 4: // 加月的
w += 31;
case 3: // 加月的
if(leapYear(year))
w += 29;
else
w += 28;
case 2: // 加月的天
w += 31;
case 1: // 1月不加了
;
}
// 得到-6,其中為星期天
w = w % 7;
// 調整星期天
if(w == 0)
w = 7;
return w;
}
// 請在下面補充代碼,實現函數printMonth
/*************** Begin **************/
void printMonth(int year,int month){
int day,i;
printf(" 一 二 三 四 五 六 日\n");
for(i=1;i<whatDay(year,month);i++){
printf(" ");
}
int temp=i-1;
int year1[13]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
int year2[13]={-1,31,29,31,30,31,30,31,31,30,31,30,31};
if(leapYear(year)){
day=year2[month];
}else{
day=year1[month];
}
for(i=whatDay(year,month);i<=day+temp;i++){
printf("%4d",i-temp);
if(i%7==0)printf("\n");
}
}
/*************** End **************/
int main()
{
// 年、月
int y, m;
// 輸入年月
cin >> y >> m;
// 輸出該年月的日歷
printMonth(y,m);
return 0;
}