#include
using namespace std;
const int months[] = {
0,31,28,31,30,31,30,31,31,30,31,30,31
};//该数组用来存储每一个月的天数
bool check(string str) {
for (int i = 0; i + 2 < str.size(); i++)
{
if (str[i + 1] == str[i] + 1 && str[i + 2] == str[i] + 2)//表示这是一个顺子
return true;
}
return false;
}
int main()
{
int year = 2022, month = 1, day = 1;
//枚举一年365天
int res = 0;//表示天数
for (int i = 0; i < 365; i++)
{
//把当前日期转化成一个八位的数字串
char str[10];
sprintf(str, "%04d%02d%02d", year, month, day);
if (check(str))
{
res++;
cout << str << endl;
}
if (++day > months[month])//如果发现天数加1之后大于当前月份的天数调用这个数组
{
day = 1;
month++;
}
}
cout << res << endl;
return 0;
}
因为数组默认是从0下标开始存储数据,你的月份是从第一月开始的,所以放一个0元素去去占据0下标的位置。这样就可以使月份从1开始,当然也可以不用放0,不过你下面使用的时候就要将下标减一。
楼上回答的正确
请采纳我
哈哈哈