算法提高 5-3日历

问题描述

  已知2007年1月1日为星期一。设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。为完成此函数,设计必要的辅助函数也是必要的。

样例输入

一个满足题目要求的输入范例。
例:

2050 3

样例输出

与上面的样例输入对应的输出。
例:


数据规模和约定

  输入数据中每一个数的范围。
  例:年 2007-3000,月:1-12。

 

//和任意年月的那道题 相差两个空格,在年份和月份之间

 


#include <stdio.h>
#include <iostream>
#include <iomanip>

using namespace std;

int MO[13] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 闰年计算函数
int leapyear(int year) {
    return ((year%4 == 0 && year%100 != 0) || year%400 == 0) ? 1 : 0;
}


int main(void)
{
    int y, m, sumy;

    cin >> y >> m;

    for(int i=2007; i<y; i++)
        if(leapyear(i))
            sumy += 366;
        else sumy += 365;
    if(m >= 2)
        if(leapyear(y))
            MO[1] += 1;
    for(int i=0; i<m-1; i++)
        sumy += MO[i];

    int t = (sumy)%7;

//    cout << t << endl;
    cout << "Calendar " << y << " - " <<  setfill('0') << setw(2)<< m << endl;
    cout << "---------------------" << endl;
    cout << " Su Mo Tu We Th Fr Sa" << endl;
    cout << "---------------------" << endl;


    for(int i=0; i<=t; i++)
    {
        cout << setfill(' ') << setw(3) << " ";
    }
    //打印日历表
    {
        for(int i=1; i<=MO[m-1]; i++)
        {
            if((t+i)%7 == 0)
                cout << endl;
            //else
            cout  << setfill(' ') << setw(3) << i;

        }

    }

    cout << endl << "---------------------" << endl;


    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值