解题思路:将年份,月份,日期各数字拆开相加,运用/ %逐位取数字。
特殊考虑闰年的2月为29天,可以另外写一个bool类型函数判断是否为闰年。
#include <iostream>
using namespace std;
bool IsSpecial(int y)//判断闰年
{
return(y%4==0&&y%100!=0||y%400==0);
}
int main()
{
// 请在此输入您的代码
int year;
int month;
int day;
int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};
int count=0;
int sum=0;
for(year=2001;year<=2021;year++)
{
if(IsSpecial(year))
arr[1]=29;
else
arr[1]=28;
for(month=1;month<=12;month++)
{
for(day=1;day<=arr[month-1];day++)
{
sum=year/1000+0+(year/10)%10+year%10+month/10+month%10+day/10+day%10;
if(sum==9||sum==16||sum==25)//最大数字加和大致为2+0+1+9+9+2+9=33<36
count++;
}
}
}
cout<<count;
return 0;
}
写给自己:对于各月份的天数,可以直接创建数组存放具体天数,不要依靠循环条件判断 。