含 2 天数
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
小蓝特别喜欢 2,今年是公元 2020 年,他特别高兴,因为每天日历上都可以看到 2。
如果日历中只显示年月日,请问从公元 1900 年 1 月 1 日到公元 9999 年 12 月 31 日,一共有多少天日历上包含 2。即有多少天中年月日的数位中包含数字 2。
AC代码
#include <iostream>
using namespace std;
bool isyear(int x)
{
return (x % 400 == 0 || x % 4 == 0 && x % 100 != 0);
}
bool check(int n)
{
while(n)
{
if(n % 10 == 2)
return true;
n /= 10;
}
return false;
}
int main()
{
int cnt = 0;
for(int i = 1900; i <= 9999; i ++)//枚举年份
{
if(check(i)) //年份包含2
{
if(isyear(i)) cnt += 366;
else cnt += 365;
}
else
{
if(isyear(i))
cnt += 180;//每个月含2天数 + 2月 + 12月
else
cnt += 179;
}
}
cout << cnt << endl;
return 0;
}