在做蓝桥杯2023年c++赛道B组日期统计,参考了这篇文章的答案:https://blog.csdn.net/t_mod/article/details/130332598
以下是代码:
#include <bits/stdc++.h>
using namespace std;
void rec(int now,int index);
int s[100] = {5,6,8,6,9,1,6,1,2,4,9,1,9,8,2,3,6,4,7,7,5,9,5,0,3,8,7,5,8,1,5,8,6,1,8,3,0,3,7,9,2,7,0,5,8,8,5,7,0,9,9,1,9,4,4,6,8,6,3,3,8,5,1,6,3,4,6,7,0,7,8,2,7,6,8,9,5,6,5,6,1,4,0,1,0,0,9,4,8,0,9,1,2,8,5,0,2,5,3,3}; //不能取array名称
int n = 100; //就这个地方,删掉就是246,留着就是235
int ans[9];
int mday[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unordered_map<string,int> mp;
void rec(int pre,int k){
if(k > 8){
string s;
int itsmouth = ans[5]*10+ans[6];
int itsday = ans[7]*10+ans[8];
if(itsmouth > 12 || itsmouth < 1){
return;
}
if(itsday > mday[itsmouth] || itsday < 1){
return;
}
for(int i=0;i<=9;i++){
s.push_back(ans[i]+'0');
}
mp[s]++;
return;
}
for(int i = pre + 1;i <= 100;i++){
if(k == 1 && s[i] == 2){
ans[k] = s[i];
rec(i,k+1);
}
else if(k == 2 && s[i] == 0){
ans[k] = s[i];
rec(i,k+1);
}
else if(k == 3 && s[i] == 2){
ans[k] = s[i];
rec(i,k+1);
}
else if(k == 4 && s[i] == 3){
ans[k] = s[i];
rec(i,k+1);
}
else if(k >=5){
ans[k] = s[i];
rec(i,k+1);
}
}
}
int main() {
rec(0,1);
cout << mp.size() << endl;
return 0;
}
奇怪的地方来了,我在开头定义的int n = 100; 原本是这样用的:
for (int i = 1; i <= n; i++) cin >> s[i];
但后来将s数组直接定义了,所以弃用了。但是一旦吧int n = 100;删除,运行结果就变成246,偏离正确答案235。如果留着,运行结果才是235。这是为什么呢?