解题思路:
个人方法:site标记每月初位置,公式就是(site+当月天数)%7,(从0开始的位置);然后就是输出,用格式化输出printf()是真的舒服。
参考代码:
#include<bits/stdc++.h> using namespace std; int m[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; #define pd_y(x) ((x%4==0&&x%100!=0)||x%400==0)?true:false //判断闰年 int main() { int year, month,site=1,t=0; cin >> year >> month; if (year < 2007) return 0; for (int i = 2007; i <year; i++) { if (!pd_y(i)) { m[2] = 28; for (int j = 1; j <= 12; j++) { site = (site + m[j]) % 7; } } else { m[2] = 29; for (int j = 1; j <= 12; j++) { site = (site + m[j]) % 7; } } } if (pd_y(year)) m[2] = 29; else m[2] = 28; for (int j = 1; j <month; j++) { site = (site + m[j]) % 7; } cout << "---------------------" << endl; cout << " Su Mo Tu We Th Fr Sa" << endl; cout << "---------------------" << endl; t = site; for (int j = site; j > 0; j--) { cout << " "; //三个空格 } for (int i = 1; i <=m[month];i++) { printf(" %2d", i); t++; if (t == 7) { cout << endl; t = 0; } if (i == m[month]&&t!=0) cout << endl; //免得两次回车 } cout << "---------------------" << endl; }
0.0分
4 人评分
C语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:552 |
A+B for Input-Output Practice (VI) (C++代码)浏览:413 |
字符串对比 (C语言代码)浏览:1398 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:624 |
【魔板】 (C++代码)(时间超限,希望会的帮我改正一下)浏览:738 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:594 |
C语言程序设计教程(第三版)课后习题11.8 (C语言代码)浏览:681 |
字符逆序 (C语言代码)浏览:612 |
数列排序 (C语言代码)浏览:606 |
小O的图案 (C语言代码)浏览:912 |