2119: 任意年月日历
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 67 Solved: 18
[ Submit][ Status][ Web Board]
Description
已知2007年1月1日为星期一。
设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。
为完成此函数,设计必要的辅助函数可能也是必要的。其中输入为年分和月份。
Input
Output
Sample Input
Sample Output
Calendar 2007-01--------------------- Su Mo Tu We Th Fr Sa--------------------- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31---------------------
HINT
Source
求输出的某年的日历,顺便说一下,这题你不遇到presentation erro的话,就牛逼了 6次的presentation Error,最后
改了自己手写的算天数的代码,取消了引用公式就对了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<list>
using namespace std;
int y,m;
int leapYear(int y)
{
if((y%4==0&&y%100!=0)||y%400==0)return 1;
return 0;
}
int main()
{
while(~scanf("%d %d",&y,&m))
{
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(y<2007)break;
int d=1;
if(leapYear(y))
{
a[2]=29;
}
for(int i=0; i<m; i++)
d+=a[i];
// int t=(y-1)+(y-1)/4+(y-1)/400-(y-1)/100+d;//公式,不知道为什么用了却不对
// t%=7;
int w = 1;//以下是自己写的求某个月的第一天是星期几的代码
for ( int i=2007 ; i<y ; i++ )
if ( leapYear(i) ) w = (w+366)%7;
else w = (w+365)%7;
for ( int i=1 ; i<m ; i++ )
w = (w+a[i])%7;//
printf("Calendar %d-%02d\n",y,m);
puts("---------------------");
puts(" Su Mo Tu We Th Fr Sa");
puts("---------------------");
for(int i=0; i<w; i++)
{
printf(" ");
}
int k=w;
for(int i=1; i<=a[m]; i++)
{
if(k==7)
{
k=0;
puts("");
}
printf("%3d",i);
k++;
}
puts("\n---------------------");
}
return 0;
}