【蓝桥杯】(完全日期)

完全日期

#include <iostream>
using namespace std;
int main()
{
    int ans=0;
    //2001 1 1 //2021 12 31
    int monthday[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    for(int year=2001;year<=2021;year++)
    {
        monthday[2]=28;
        if((year%4==0&&year%100!=0)||year%400==0)
        monthday[2]=29;
        int cnt1=0;
        int tmp=year;
        while(tmp)
        {
           // tmp%=10;
            cnt1+=tmp%10;
            tmp/=10;
        }
    for(int month=1;month<=12;month++)
    {
        for(int day=1;day<=monthday[month];day++)
        {
            int cnt2=month/10+month%10;
            int cnt3=day/10+day%10;
            int cnt=cnt1+cnt2+cnt3;
            if(cnt==9||cnt==16||cnt==25)
            ans++;
        }
    }
    }
    cout<<ans;
  // 请在此输入您的代码
  return 0;
}

错误版本(只处理个位数):

tmp = 2021; // 初始值为年份2021

// 第一次循环
tmp %= 10; // tmp = 2021 % 10 = 1
cnt1 += tmp; // cnt1 = 0 + 1 = 1
tmp /= 10; // tmp = 1 / 10 = 0 (向下取整)

// 第二次及后续循环,因tmp已变为0,循环终止

在这个例子中,错误版本的代码只处理了年份2021的个位数1,并将1累加到cnt1。由于tmp在第一次循环后被除以10后变为0,循环不再继续,因此cnt1最终只包含了年份的个位数之和,即1

正确版本(递归处理所有位数):

tmp = 2021; // 初始值为年份2021

// 第一次循环
cnt1 += tmp % 10; // cnt1 = 0 + 1 = 1
tmp /= 10; // tmp = 2021 / 10 = 202 (向下取整)

// 第二次循环
cnt1 += tmp % 10; // cnt1 = 1 + 2 = 3
tmp /= 10; // tmp = 202 / 10 = 20 (向下取整)

// 第三次循环
cnt1 += tmp % 10; // cnt1 = 3 + 0 = 3
tmp /= 10; // tmp = 20 / 10 = 2 (向下取整)

// 第四次循环
cnt1 += tmp % 10; // cnt1 = 3 + 2 = 5
tmp /= 10; // tmp = 2 / 10 = 0 (向下取整)

// 第五次及后续循环,因tmp已变为0,循环终止
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值