好的,我很抱歉我最初的声明,但是当您希望它按照您在我的第一个答案的评论中描述的那样工作时,您实际上需要重新排序数据......好吧。它可以在没有辅助矩阵的情况下完成,但是生成的代码可能非常复杂,只要矩阵只使用几个字节的内存,为什么不使用这个小辅助构造呢?
我的代码在下面做的是创建一个矩阵。我们从上到下然后从左到右编写矩阵(当我们用完元素来填充第一行的所有列时,停止填充除第一行以外的任何内容)。然后我们以不同的顺序阅读它,从左到右,从上到下。基本上我们在这里做的是转置一个矩阵, 以一种顺序写入,但以另一种顺序读取。转置矩阵是一个非常基本的数学运算(许多 3D 编程通过使用矩阵计算来工作,转置实际上是一个简单的运算)。诀窍是我们最初如何填充矩阵。为了确保我们在任何情况下都可以填充第一列,与所需的列数和数组的大小无关,如果我们用完元素,我们必须停止按正常顺序填充矩阵并保留所有剩余的元素第一排。这将产生您在评论中建议的输出。
老实说,整个事情有点复杂,但它背后的理论应该是理智的,而且效果很好:-D
int Columns;
char * Array[] = {"A", "B", "C", "D", "E", "F", "G"};
int main (
int argc,
char ** argv
) {
// Lets thest this with all Column sizes from 1 to 7
for (Columns = 1; Columns <= 7; Columns++) {
printf("Output when Columns is set to %d\n", Columns);
// This is hacky C for quickly get the number of entries
// in a static array, where size is known at compile time
int arraySize = sizeof(Array) / sizeof(Array[0]);
// How many rows we will have
int rows = arraySize / Columns;
// Below code is the same as (arraySize % Columns != 0), but
// it's almost always faster
if (Columns * rows != arraySize) {
// We might have lost one row by implicit rounding
// performed for integer division
rows++;
}
// Now we create a matrix large enough for rows * Columns
// references. Note that this array could be larger than arraySize!
char ** matrix = malloc(sizeof(char *) * rows * Columns);
// Something you only need in C, C# and Java do this automatically:
// Set all elements in the matrix to NULL(null) references
memset(matrix, 0, sizeof(char *) * rows * Columns );
// We fill up the matrix from top to bottom and then from
// left to right; the order how we fill it up is very important
int matrixX;
int matrixY;
int index = 0;
for (matrixX = 0; matrixX < Columns; matrixX++) {
for (matrixY = 0; matrixY < rows; matrixY++) {
// In case we just have enough elements left to only
// fill up the first row of the matrix and we are not
// in this first row, do nothing.
if (arraySize + matrixX + 1 - (index + Columns) == 0 &&
matrixY != 0) {
continue;
}
// We just copy the next element normally
matrix[matrixY + matrixX * rows] = Array[index];
index++;
//arraySize--;
}
}
// Print the matrix exactly like you'd expect a matrix to be
// printed to screen, that is from left to right and top to bottom;
// Note: That is not the order how we have written it,
// watch the order of the for-loops!
for (matrixY = 0; matrixY < rows; matrixY++) {
for (matrixX = 0; matrixX < Columns; matrixX++) {
// Skip over unset references
if (matrix[matrixY + matrixX * rows] == NULL)
continue;
printf("%s", matrix[matrixY + matrixX * rows]);
}
// Next row in output
printf("\n");
}
printf("\n");
// Free up unused memory
free(matrix);
}
return 0;
}
输出是
Output when Columns is set to 1
A
B
C
D
E
F
G
Output when Columns is set to 2
AE
BF
CG
D
Output when Columns is set to 3
ADG
BE
CF
Output when Columns is set to 4
ACEG
BDF
Output when Columns is set to 5
ACEFG
BD
Output when Columns is set to 6
ACDEFG
B
Output when Columns is set to 7
ABCDEFG
这段 C 代码应该很容易移植到 PHP、C#、Java 等,没有什么大魔法,所以它几乎是通用的、可移植的和跨平台的。
我应该补充一件重要的事情:
如果将 Columns 设置为零(除以零,我不检查),此代码将崩溃,但是 0 Columns 有什么意义呢?如果您的列数多于数组中的元素,它也会崩溃,我也不检查这一点。获得 arraySize 后,您可以轻松地检查其中任何一个:
if (Columns <= 0) {
// Having no column make no sense, we need at least one!
Columns = 1;
} else if (Columns > arraySize) {
// We can't have more columns than elements in the array!
Columns = arraySize;
}
此外,您还应该检查 arraySize 是否为 0,在这种情况下,您可以直接跳出该函数,因为在这种情况下,该函数绝对无事可做 :) 添加这些检查应该使代码坚如磐石。
顺便说一句,在数组中有 NULL 元素将起作用,在这种情况下,结果输出中没有漏洞。NULL 元素就像不存在一样被跳过。例如让我们使用
char * Array[] = {"A", "B", "C", "D", "E", NULL, "F", "G", "H", "I"};
输出将是
ADFI
BEG
CH
对于 Columns == 4。如果你想要孔,你需要创建一个孔元素。
char hole = 0;
char * Array[] = {"A", "B", &hole, "C", "D", "E", &hole, "F", "G", "H", "I"};
并稍微修改绘画代码
for (matrixY = 0; matrixY < rows; matrixY++) {
for (matrixX = 0; matrixX < Columns; matrixX++) {
// Skip over unset references
if (matrix[matrixY + matrixX * rows] == NULL)
continue;
if (matrix[matrixY + matrixX * rows] == &hole) {
printf(" ");
} else {
printf("%s", matrix[matrixY + matrixX * rows]);
}
}
// Next row in output
printf("\n");
}
printf("\n");
输出样本:
Output when Columns is set to 2
A
BF
G
CH
DI
E
Output when Columns is set to 3
ADG
BEH
I
CF
Output when Columns is set to 4
AC H
BDFI
EG