算法提高 5-3日历
时间限制:1.0s 内存限制:256.0MB
问题描述
已知2007年1月1日为星期一。设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。为完成此函数,设计必要的辅助函数也是必要的。
样例输入
一个满足题目要求的输入范例。
例:
2050 3
例:
2050 3
样例输出
与上面的样例输入对应的输出。
例:
例:
![](https://i-blog.csdnimg.cn/blog_migrate/29404420c0cfada2d2747c8b508e2946.png)
数据规模和约定
输入数据中每一个数的范围。
例:年 2007-3000,月:1-12。
例:年 2007-3000,月:1-12。
代码实现:
#include<bits/stdc++.h>
using namespace std;
//判断平闰年
bool py(int y){
if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0){
return true;
}
return false;
}
int main(){
int y, m;
cin >> y >> m;
int p[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int r[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i, j;
int differ_days = 0;
for(i = 2007; i < y; i ++){
if(py(i)){
differ_days += 366;
}
else{
differ_days += 365;
}
}
for(j = 1; j < m; j ++){
if(py(y)){
differ_days += r[j];
}
else{
differ_days += p[j];
}
}
int c = differ_days % 7 + 1;
if(c == 7){
c = 0;
}
cout << "Calendar " << y << " - ";
if(m < 10){
cout << "0";
}
cout << m << endl;
cout << "---------------------" << endl;
cout << "Su Mo Tu We Th Fr Sa" << endl;
cout << "---------------------" << endl;
int st = 1;
for(i = 0; i < c; i ++){
cout << " ";
}
for(i = c; i < 7; i ++){
printf("%2d ", st);
if(py(y)){
if(st == r[m]){
break;
}
}
else{
if(st == p[m]){
break;
}
}
st ++;
while(i == 6){
i = -1;
cout << endl;
}
}
cout << endl << "---------------------";
}