第十一届蓝桥杯大赛第二场省赛试题c++b组 D:跑步锻炼 答案+解析

在这里插入图片描述

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;

int mouths[13] ={0,31,28,31,30,31,30,31,31,30,31,30,31};

int cnt = 5;    //星期几
int ans = 0;    //答案

bool check(int date)    //检查当前日期是否合法
{
    int year = date/10000;
    int mouth = date%10000/100;
    int day = date%100;
    
    if(mouth<1||mouth>12)return false;
    if(day<1)return false;
    bool leap = (year%4==0&&year%100!=0)||(year%400==0);   //判断是闰年还是平年
    if(mouth!=2&&day>mouths[mouth])return false;    //不是二月份是否合法
    if(mouth==2&&day>mouths[mouth]+leap)return false;   //是二月份是否合法
    return true;
}

int main()
{
    
    int start = 20000101;
    for(int i=start;i<=20201001;i++)
    {
        if(check(i))    //检查i是否合法
        {
            cnt++;  //判断星期几
            ans++;  //累计结果
            int day = i%100;    //计算当月天数
            if(cnt%7==1||day==1)ans++;  //如果是周一或者月初,结果再加1;
        }
    }
    cout<<ans<<endl;
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kaiaaaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值