蓝桥杯真题训练

该篇文章讲述了如何在给定的100位数字数组中,通过编程找到满足条件的8位子序列,形成2023年的日期,如20230902等,通过定义判断函数和使用C++编程实现计数并去除重复。
摘要由CSDN通过智能技术生成

试题A:日期统计

【问题描述】

小蓝现在有一个长度为 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

现在他想要从这个数组中寻找一些满足以下条件的子序列:

子序列的长度为 8;

这个子序列可以按照下标顺序组成一个 yyyymmdd 格式的日期,并且

要求这个日期是 2023 年中的某一天的日期,例如 20230902,20231223。

yyyy 表示年份,mm 表示月份,dd 表示天数,当月份或者天数的长度只

有一位时需要一个前导零补充。

请你帮小蓝计算下按上述条件一共能找到多少个不同 的 2023 年的日期。

对于相同的日期你只需要统计一次即可。

观察寻找规律,做了简单的剪枝。

#include<iostream>

#include<vector>

#include<string>

#include <algorithm>

using namespace std;

bool panduan(string a)

{if(a[0]=='0')

{  if(a[1]=='0')

return false;

if(a[2]=='0'&&a[3]=='0')

        return false;

        if(a[1]=='2')

        {

            if(a[2]=='0'||a[2]=='1')

            {

                return true;

            }

            else if(a[2]=='2')

            {

                if(a[3]=='9')

                    return false;

                else

                    return true;

            }

            else

                return false;

        }

        else if(a[1]=='1'||a[1]=='3'||a[1]=='5'||a[1]=='7'||a[1]=='8')

        {

            if(a[2]=='0'||a[2]=='1'||a[2]=='2')

            {

                return true;

            }

            else if(a[2]=='3')

            {

                if(a[3]!='1'&&a[3]!='0')

                    return false;

                else

                    return true;

            }

            else

                return false;

        }

        else

        {

            if(a[2]=='0'||a[2]=='1'||a[2]=='2')

            {

                return true;

            }

            else if(a[2]=='3')

            {

                if(a[3]!='0')

                    return false;

                else

                    return true;

            }

            else

                return false;

        }

}

else if(a[0]=='1')

{if(a[2]=='0'&&a[3]=='0')

        return false;

    if(a[1]=='0'||a[1]=='2')

    {

        if(a[2]=='0'||a[2]=='1'||a[2]=='2')

            {

                return true;

            }

            else if(a[2]=='3')

            {

                if(a[3]!='1'&&a[3]!='0')

                    return false;

                else

                    return true;

            }

            else

                return false;

    }

    else if(a[1]=='1')

    {

        if(a[2]=='0'||a[2]=='1'||a[2]=='2')

            {

                return true;

            }https://www.fosshub.com/Code-Blocks.html

            else if(a[2]=='3')

            {

                if(a[3]!='0')

                    return false;

                else

                    return true;

            }

            else

                return false;

    }

    else

        return false;

}

else

    return false;

}

int main(){

int arr[] = {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};

vector<string> aa;

int c;

for(int i=0;i<100;i++)

{

    if(arr[i]==2)

    {

        c=i;

        break;

    }

}

for(int i=c+1;i<100;i++)

{

    if(arr[i]==0)

    {

        c=i;

        break;

    }//cout<<c<<endl;

//cout<<panduan("1202");

}

for(int i=c+1;i<100;i++)

{

    if(arr[i]==2)

    {

        c=i;

        break;

    }

}

for(int i=c+1;i<100;i++)

{

    if(arr[i]==3)

    {

        c=i;

        break;

    }

}

//cout<<c<<endl;

//cout<<panduan("1202");

for(int i=c+1;i<100;i++)

{

    for(int j=i+1;j<100;j++)

    {

for(int p=j+1;p<100;p++)

        {

            for(int q=p+1;q<100;q++)

            {   string riqi="";

                riqi=riqi+char(48+arr[i])+char(48+arr[j])+char(48+arr[p])+char(48+arr[q]);

              if(panduan(riqi))

              {cout<<riqi<<endl;

                aa.push_back(riqi);

              }

          }

       }

    }

}

cout<<"总数为:"<<aa.size()<<endl;

sort(aa.begin(), aa.end()); // 先对 vector 进行排序

vector<string>::iterator it = unique(aa.begin(), aa.end()); // 将重复元素移到 vector 后面

aa.erase(it, aa.end()); // 删除重复元素后面的部分

cout<<"去重后总数为:"<<aa.size()<<endl;

}

主要学到了 vector基本用法vector<string>::iterator it指针的使用  unique函数的使用erase的使用。采用的全部暴力求解,编写判断函数来选择的方法。

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值