测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A40213. 含2天数

填空题 困难

题目描述

含2天数

题目描述

小蓝特别喜欢 2,今年是公元 2020 年,他特别高兴,因为每天日历上都可以看到 2。

如果日历中只显示年月日,请问从公元 1900 年 1 月 1 日到公元 9999 年 12月 31 日,一共有多少天日历上包含 2。

即有多少天中年月日的数位中包含数字 2。

答案提交

这是一道结果填空的题,你只需要算出结果后提交即可。

本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

参考答案

#include <iostream> using namespace std; int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; bool is_leap(int year) { return year % 400 == 0 || year % 4 == 0 && year % 100 != 0; } bool check1(int x) { int year = x / 10000; int month = x / 100 % 100; int day = x % 100; if(month < 1 || month > 12) return false; if(month != 2) { if(day < 1 || day > days[month]) return false; } else { if(day < 1 || day > days[month] + is_leap(year)) return false; } return true; } bool check2(int x) { while(x) { if(x % 10 == 2) return true; x /= 10; } return false; } int main() { int ans = 0; for (int i = 19000101; i <= 99991231; i ++) if(check1(i) && check2(i)) ans ++; cout << ans << endl; return 0; }

答案解析

答案:1994240

上一题 下一题