例如,2015 年 1 月的月历打印效果如下(第一列为周日):
#include <iostream> using namespace std; const int dayNum[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int m, offset, i; int main( ) { cin >> m; cout << "S\tM\tT\tW\tT\tF\tS" << endl; // '\t'为 TAB制表符 1; for (i = 1; i < m; i++) offset = 2; for (i = 0; i < offset; i++) cout << '\t'; for (i = 1; i <= 3; i++) { cout << 4; if (i == dayNum[m] || 5 == 0) cout << endl; else cout << '\t'; } return 0; }