完善程序-1解析

 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
32
33
34
35
36
37
/**************************************************************** 
 * Description:  2015_J_27
 * Author: Alex Li
 * Date: 2021-09-05 11:38:48
 * LastEditTime: 2023-09-06 12:59:25
****************************************************************/
#include <iostream>
using namespace std;
//每个月有几天,dayNum[0]=-1,就是月份从1开始
const int dayNum[]={-1,31,28,31,30,31,30,31,31,30,31,30,31};
//m代表月份
int m,offset,i;
int main(){
    cin>>m;
    //打印表头:  S     M      T     W     T     F     S 
    cout<<"S\tM\tT\tW\tT\tF\tS"<<endl; // 
    //1月份前面空了3个\t
    offset=4;  
    //根据月份,计算offset的值
    for ( i = 1; i < m; i++)
        offset=(offset+dayNum[i])%7;
    //根据offset的值打印'\t'
    for ( i = 0; i < offset; i++)
        cout<<'\t';
    //输出m月份的日历
    for ( i = 1; i <= dayNum[m]; i++){
     //i是具体日期,每个月的天数在dayNum数组里
        cout<<i;
         //每7天换行
        if (i==dayNum[m]||(offset+i)%7==0)
            cout<<endl; 
        //不到7天就用空'\t'
        else
            cout<<'\t';
    }
    return 0;
}
Scroll to Top