日期组合统计

小猴子有一个长度为100的数组,数组中每个元素的值都在0到9的范围之内。数组中的元素从左到右如下:

5686916124919823647759503875815861830379270588570991944686338516346707827689565614010094809128502533

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

(1)子序列的长度为8。

(2)这个子序列可以按照下标顺序组成一个yyyymmdd格式的日期,并且要求这个日期是2023年中某一天的日期,例如20230902、20231223。yyyy表示年份,mm表示月份,dd表示天数,当月份或者天数的长度只有一位时需要用一个前导零补充。

请问,按上述条件一共能找到多少个不同的2023年的日期。对于相同的日期,只需要统计一次即可。

 

生成 2023 年的 365 个日期,然后检查有多少个能在 100 个数字中找到。

#include

using namespace std;

int num[110];

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {

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

    int ans = 0;

    for (int i = 1; i <= 12; i++) {

        for (int j = 1; j <= days[i]; j++) {

            string str = "2023"; //年

            if (i < 10) str += "0";

            str += to_string(i); //+月

            if (j < 10) str += "0";

            str += to_string(j); //+日

            int k = 0;

            for (int v = 0; v < 100 && k < 8; v++) //在 100 个数中找这个日期

                if (num[v] == str[k] - '0')

                    k++;

            if (k >= 8) ans++;

        }

    }

    cout << ans << endl;

    return 0;

}

 

你可能感兴趣的:(c++)