对声明这种数组感到困惑

Confused of declaring this kind of array

本文关键字:数组 声明      更新时间:2023-10-16

我正在阅读这个代码

有一条线路:pair <int, int> approach[1 << 18][17]

我不知道这个声明的含义:approach[ 1<<18 ][17];

有人能帮忙吗?

在此上下文中,<<是位左移运算符。1 << 18表示取1的二进制表示并将其向左移动18。这是218(2的幂18,或262144)。所以你有一个非常大的二维阵列对:

pair <int, int> approach[262144][17];

<<是位左移运算符。

所以1 << 18是一个整数常数,其值为218

它简单地表示2^18,2的18次方。

代码缺少一些解释,唯一真正好的信息是

// SGU 502 -- Digits Permutation

啊,这是关于数字排列的,所以

pair <int, int> approach[1 << 18][17]

可以用来存储排列,除非对排列有一些限制,否则排列的数量应该是N!(希望是N!<=(1<<18))。

但这个定义并没有说明这一点,让我们看看是否可以让它更清楚(希望是正确的)。

const int maxLength = 17;
const int maxPermutation = 1 << (maxLength+1);
pair <int, int> approach[maxPermutation ][maxLength]
static_assert(factorial(maxLength) <= maxPermutation, "approach might not be able to hold all permutations");