蓝桥杯2023(十四届)省赛——统计日期(八重神子)
·
统计日期
其实一开始我想直接暴力的,然后写着写着突然觉得可以优化一下: 优化方法:先找所有2023的位置,记录初始和最后的位置 找出所有合法日期的位置,使用前缀和,计算以当前2023结尾时,其后有几个合法日期,相加即可 但是有个问题就是,如何如果遇见相同的日期,怎么存储其位置?
最后还是用的八重神子(我不是OP):注意筛选还有月和日不能是00
注意,下面的代码只是用来计算的,不能作为AC代码使用,因为本题是填空题,所以超时无所谓
#include <iostream>
#include <set>
using namespace std;
int str[101];
int m[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
set<int>s;
bool check(int a, int b, int c, int d)
{
//只要确定月份是否匹配就行
int month = 10 * a + b;
int day = 10 * c + d;
if (month > 12 || month==0) return false;
if (day > m[month] || day==0) return false;
return true;
}
int main()
{
for (int i = 0; i < 100; i++) cin >> str[i];
//优化方法:先找所有2023的位置,记录初始和最后的位置
//找出所有合法日期的位置,使用前缀和,计算以当前2023结尾时,其后有几个合法日期,相加即可
//但是有个问题就是,如何如果遇见相同的日期,怎么存储其位置?
// 考虑了这么多,我是真的没想到,直接用了一个八重神子
for (int i = 0; i < 100; i++) //2
if(str[i]==2)
for (int j = i + 1; j < 100; j++) //0
if(str[j]==0)
for (int k = j + 1; k < 100; k++) //2
if(str[k]==2)
for (int a = k + 1; a < 100; a++) //3
if (str[a] == 3)
for(int b=a+1;b<100;b++) //月1
if (str[b] == 0 || str[b] == 1)
for (int c = b + 1; c < 100; c++) //月2
{
if (str[b] == 1 && str[c] > 2) continue;
for (int d = c + 1; d < 100; d++) //天1
{
if (str[d] > 3) continue;
for (int e = d + 1; e < 100; e++) //天2
{
if (check(str[b], str[c], str[d], str[e]))
{
int temp =1e7 * str[i] + 1e6 * str[j] + 1e5 * str[k] + 1e4 * str[a] + 1e3 * str[b] + 1e2 * str[c] + 1e1 * str[d] + str[e];
s.insert(temp);
}
}
}
}
/*for (auto it : s)
cout << it << endl;*/
cout << s.size(); //235
return 0;
}
更多推荐


所有评论(0)