我正在尝试创建一个从字符列表填充数组的函数,以便该数组将由n
个长度唯一的字符串组成。应包括所有可能的排列。下面的工作示例使用n = 2
,但是我希望能够在运行时更改n
。
static char ULC[62] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
static char pw[4096][2];
for (int i = 0; i < 62; i++)
{
for (int j = 0; j < 62; j++)
{
pw[i * 62 + j][0] = ULC[i];
pw[i * 62 + j][1] = ULC[j];
}
}
显然增加n
会需要更大的数组,但在上面的示例中我只是使用静态数组来简化代码和说明。理想情况下,某些东西可以在Visual Studio C中使用(不是C ++,绝对不是python,java等)。
答案 0 :(得分:1)
如果您这样重写它,则使其完全动态化应该相当简单:
const int num_chars = 62;
const int n = 3;
static char pwn[num_chars * num_chars * num_chars][n];
for (int i = 0; i < num_chars * num_chars * num_chars; ++i) {
int val = i;
for (int in = 0; in < n; ++in) {
pwn[i][in] = ULC[val % num_chars];
val /= num_chars;
}
}