第一次写博客,有错误感谢指正哈
题目:
思路:
比赛时看到这题也是有点懵逼,但细想一下题目给出了三个指示条件:
①从数组中找到规定条件的子序列:说明找元素是有先后顺序的,而不是直接从数组里挑;
②对于相同的日期只需统计一次:说明需要对符合要求的序列进行去重,看到这个我也是灵光一闪想到了利用集合(set)进行存储,利用集合的互异性可以省去很多麻烦;
③这是一道填空题,只需提交答案即可:非常nice,这样就不要纠结想什么算法降低时间花费了,直接暴力求解,在编辑器上得出结果直接提交即可。
代码:
#include<bits/stdc++.h>
int main(){
using namespace std;
//规定2023年的各月份最大天数(平年28天)
int max_day[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//存放数据
int data[100] = {0};
set<string> count_data;//用于存储目标日期的集合
char ch[4];//临时存储
//输入
for(int i=0;i<100;i++){
scanf("%d",&data[i]);
}
int count = 0;
//数据处理——20230101
for(int year1=0; year1<100; year1++){
if( data[year1] != 2){
continue;
}
for(int year2=year1+1;year2<100;year2++){
if( data[year2] != 0){
continue;
}
for(int year3=year2+1;year3<100;year3++){
if( data[year3] != 2){
continue;
}
for(int year4=year3+1;year4<100;year4++){//这里前4个for循环用于筛选年份是否为2023
if( data[year4] != 3){
continue;
}
for(int m1=year4+1;m1<100;m1++){//后4个for循环对月份及日期进行处理
for(int m2=m1+1;m2<100;m2++){
for(int d1=m2+1;d1<100;d1++){
for(int d2=d1+1;d2<100;d2++){
//对月份进行处理
int month = data[m1]*10+data[m2];
if(month<1 || month>12){//1-12,不是则跳过
continue;
}
int date = data[d1]*10 + data[d2];
if(date>max_day[month] || date<1){//1-最大天数,不是则跳过
continue;
}
//以上条件都满足,说明找到了规定的有序序列,将其存入集合中存储
/*
注:集合是一个不能出现相同元素的存储容器,可以帮助我们
自动去重。又因为题目已经规定好了年份,所以我们只要将月和
日存进去即可
%02d:存入2位,不足用0补齐,以符合mmdd的格式
*/
sprintf(ch, "%02d%02d", month, date);
count_data.insert(ch);
}
}
}
}
}
}
}
}
printf("%d",count_data.size());
return 0;
/*
输入数据:
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
*/
}
答案:
235