HW03-输出指定年份的日历

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int year;
    cin >> year;

    //确定当前年份1月1日是星期几
    int start = 0;
    for (int i = 2023; i < year; i++)
    {
        if (i % 4 == 0) start += 2;
        else start += 1;
    }
    start = start % 7;
    if (start == 0) start += 7;
    
    //数组初始化为0
    int c[53][8];
    for (int i = 0; i < 53; i++)
        for (int j = 0; j < 8; j++)
            c[i][j] = 0;

    //生成日期
    int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (year % 4 == 0) a[2] = 29;
    int p = 0, q = start;
    for (int m = 1; m <= 12; m++)
    {
        for (int i = 1; i <= a[m]; i++)
        {
            c[p][q] = i;
            if (q == 7)
            {
                q = 1;
                p++;
            }
            else
                q++;
        }
    }
    
    //生成月份
    int month = 1;
    for (int i = 0; i < 53; i++)
        for (int j = 1; j < 8; j++)
            if (c[i][j] == 1)
            {
                c[i][0] = month;
                month++;
                break;
            }

    //输出
    for (int i = 0; i < 53; i++)
    {
        if(c[i][0] == 0) cout << "  ";
        else cout << setw(2) << setfill('0') << c[i][0];

        for (int j = 1; j < 8; j++)
        {
            if (c[i][j] != 0) cout << setw(3) << setfill(' ') << c[i][j];
            else cout << "   ";
        }

        cout << endl;
    }

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值