打印日历
题意:
输入年份和月份,打印出此年此月的日历.
思路:
利用蔡乐公式,计算出Y年M月1号是星期几,然后直接打印输出就好.
代码:
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <vector> using namespace std; typedef long long LL; int main(){ //freopen("input.txt", "r", stdin); int YY, MM; int kas = 1; while(~scanf("%d%d", &YY, &MM)){ int Y = YY, M = MM; int C, D = 1; C = Y / 100, Y %= 100; if(M < 3) M += 12, Y -= 1; int W = (Y + Y / 4 + C / 4 - 2 * C + 26 * (M + 1) / 10 + D - 1) % 7; while(W < 0) W += 7; // printf("%d\n", W); printf("Case %d:\n", kas++); printf(" Sun Mon Tue Wed Thu Fri Sat\n"); int day = 1; int mday = -1; if(MM == 2){ if( (YY % 4 == 0 && YY % 100 != 0 )|| ( YY % 400 == 0) ) mday = 29; else mday = 28; } else{ if(MM == 1 || MM == 3 || MM == 5 || MM == 7 || MM == 8 || MM == 10 || MM == 12)mday = 31; else mday = 30; } for(int i = 0; i < 6; i++){ int flag = 0; for(int j = 0; j < 7; j++){ if(W != 0){ printf(" "); W--; } else{ printf("%5d", day++); if(day == mday + 1){ flag = 1; break; } } } printf("\n"); if(flag) break; } } return 0; }
It's not numeral,character and punctuation .It's my life.