问题描述
已知2007年1月1日为星期一。设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。为完成此函数,设计必要的辅助函数也是必要的。
样例输入
一个满足题目要求的输入范例。
例:
2050 3
样例输出
与上面的样例输入对应的输出。
例:
数据规模和约定
输入数据中每一个数的范围。
例:年 2007-3000,月:1-12。
//和任意年月的那道题 相差两个空格,在年份和月份之间
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int MO[13] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 闰年计算函数
int leapyear(int year) {
return ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? 1 : 0;
}
int main(void)
{
int y, m, sumy;
cin >> y >> m;
for(int i=2007; i<y; i++)
if(leapyear(i))
sumy += 366;
else sumy += 365;
if(m >= 2)
if(leapyear(y))
MO[1] += 1;
for(int i=0; i<m-1; i++)
sumy += MO[i];
int t = (sumy)%7;
// cout << t << endl;
cout << "Calendar " << y << " - " << setfill('0') << setw(2)<< m << endl;
cout << "---------------------" << endl;
cout << " Su Mo Tu We Th Fr Sa" << endl;
cout << "---------------------" << endl;
for(int i=0; i<=t; i++)
{
cout << setfill(' ') << setw(3) << " ";
}
//打印日历表
{
for(int i=1; i<=MO[m-1]; i++)
{
if((t+i)%7 == 0)
cout << endl;
//else
cout << setfill(' ') << setw(3) << i;
}
}
cout << endl << "---------------------" << endl;
return 0;
}