打卡

C++ ">>"表示右移,右移一位(相当于除以2)

简单了解了下dfs算法,在C++中熟练掌握会很方便。可以使用ios::sync_with_stdio(0);cin.tie(0);提高运行效率;

最后附上蓝桥题解源码:

小蓝现在有一个长度为 100 的数组,数组中的每个元素的值都在 0 到 9 的 范围之内。数组中的元素从左至右如下所示: 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
现在他想要从这个数组中寻找一些满足以下条件的子序列:
1. 子序列的长度为 8;
2. 这个子序列可以按照下标顺序组成一个 yyyymmdd 格式的日期,并且要求这个日期是 2023 年中的某一天的日期,例如 20230902,231223。 yyyy 表示年份,mm 表示月份,dd 表示天数,当月份或者天数的长度只有一位时需要一个前导零补充。请你帮小蓝计算下按上述条件一共能找到多少个不同 的 2023 年的日期。对于相同的日期你只需要统计一次即可。

#include<bits/stdc++.h>
using namespace std;
int a[100],ans;
bool vis[20240000];
bool check(int date){
    if(vis[date]) return false;
    vis[date]=1;
    int mm=date/100%100;
    int dd=date%100;
    if(mm<1||12<mm) return false;
    if(mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12) {
        if(1<=dd&&dd<=31) return true;
    }else if(mm==2)
    {
        if(1<=dd&&dd<=28) return true;
    }
    else if(1<=dd&&dd<=30) return true;
    else return false;
}
void dfs(int x,int pos,int date)
{
    if(x==100) return;
    if(pos==8)
    {
        if(check(date)) ++ans;
        return;
    }
    if((pos==0&&a[x]==2)||
    (pos==1&&a[x]==0)||
    (pos==2&&a[x]==2)||
    (pos==3&&a[x]==3)||
    (pos==4&&0<=a[x]&&a[x]<=1)||
    (pos==5&&0<=a[x]&&a[x]<=9)||
    (pos==6&&0<=a[x]&&a[x]<=3)||
    (pos==7&&0<=a[x]&&a[x]<=9))
    dfs(x+1,pos+1,date*10+a[x]);
    dfs(x+1,pos,date);
 } 

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    for(int i=0;i<100;++i)cin>>a[i];
    dfs(0,0,0);
    cout<<ans;
    return 0;
}
/*
1 2 3 4 5 6 7 8 9 0 
*/

 

posted on 2024-01-17 22:09  清荣峻茂  阅读(22)  评论(0)    收藏  举报