输出日历:
1 #include<iostream> 2 3 using namespace std; 4 5 int mm[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 6 7 // 闰年判断 8 bool isLeap(int y) 9 { 10 return ((y%4 == 0 && y%100 != 0) || (y%400 == 0)); 11 } 12 13 // 基姆拉尔森计算公式 14 int week(int y, int m, int d) 15 { 16 if(m == 1 || m == 2){ 17 m += 12;y--; 18 } 19 int r = (d + 2*m + 3*(m+1)/5 + y + y/4 - y/100 + y/400 + 1)%7; 20 return (r == 0)?7:r; 21 } 22 23 void cal(int year) 24 { 25 if(isLeap(year)) mm[2] = 29; 26 for(int i=1;i<=12;i++){ 27 cout<<"---Mouth:"<<i<<"--- "; 28 int s = week(year, i, 1)%7; 29 for(int t=0;t<=s-1;t++) cout<<" "; 30 for(int j=1;j<=mm[i];j++){ 31 cout<<j<<' '; 32 if(!((++s)%7)) cout<<' '; 33 } 34 cout<<" ----------------------------------------------------- "; 35 } 36 } 37 38 int main() 39 { 40 int y, m, d; 41 // while(cin>>y>>m>>d) 42 while(cin>>y) 43 { 44 // 检测 45 /* 46 int s = 1; 47 for(int i=1;i<=12;i++){ 48 for(int j=1;j<=mm[i];j++){ 49 cout<<week(y, i, j)<<' '; 50 if(s%7 == 0) cout<<' '; 51 s++; 52 } 53 } 54 */ 55 // cout<<week(y, m, d)<<' '; 56 cal(y); 57 58 } 59 60 return 0; 61 }
2019-03-19
23:27:48