這是以螺旋方式遍歷矩陣并將遍歷列印為 Python 中的陣列的演算法:
def spiralTraverse(array):
result = []
startRow, endRow = 0, len(array) - 1
startCol, endCol = 0, len(array[0]) - 1
while startRow <= endRow and startCol <= endCol:
for col in range(startCol, endCol 1):
result.append(array[startRow][col])
for row in range(startRow 1, endRow 1):
result.append(array[row][endCol])
for col in reversed(range(startCol, endCol)):
if startRow == endRow:
break
result.append(array[endRow][col])
for row in reversed(range(startRow 1, endRow)):
if startCol == endCol:
break
result.append(array[row][startCol])
startRow = 1
endRow -= 1
startCol = 1
endCol -= 1
return result
所以如果樣本輸入是
array = [[1, 2, 3, 4],[12, 13, 14, 5],[11, 16, 15, 6],[10, 9, 8, 7]]
輸出將是
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
我正在嘗試使用指標將其轉換為 C,但該函式無法列印最終陣列。該檔案可以編譯,但帶有與多維陣列相關的警告,作為螺旋Traverse()中的引數。我從main()呼叫螺旋遍歷函式如下:,其中和分別是行數和列數。spiralTraverse(*array[r], r, c)r=rowsc=cols
這就是我所做的:
void spiralTraverse(int *vector[], int rows, int cols)
{
int i, indx_col, indx_row;
int indx_array = 0;
int startRow = 0;
int endRow = rows-1;
int startCol = 0;
int endCol = cols-1;
int new_array[rows*cols];
while ((startRow <= endRow) && (startCol <= endCol))
{
for (indx_col=startCol;indx_col<endCol 1;indx_col )
new_array[indx_array ] = *(*(vector startRow) indx_col);
for (indx_row=startRow 1;indx_row<endRow 1;indx_row )
new_array[indx_array ] = *(*(vector indx_row) endCol);
for (indx_col=endCol;indx_col>startCol;indx_col--)
{
if (startRow == endRow)
break;
new_array[indx_array ] = *(*(vector endRow) indx_col);
}
for (indx_row=endRow;indx_row>startRow 1;indx_row--)
{
if (startCol == endCol)
break;
new_array[indx_array ] = *(*(vector indx_row) startCol);
}
startRow ;
endRow--;
startCol ;
endCol--;
}
puts("");
puts("The array below displays the elements in spiral order");
for (i=0;i<(rows*cols);i )
printf("%d, ", new_array[i]);
puts("");
}
}
為什么程式無法列印我的最終一維陣列?
uj5u.com熱心網友回復:
函式引數
該檔案編譯但帶有與多維陣列相關的警告作為
spiralTraverse().
有關在 C 中分配和傳遞多維陣列作為函式引數的正確方法的詳細資訊,請參閱其他問答:
正確分配多維陣列
VLA 到底有什么意義?
如何將多維陣列傳遞給 C 和 C 中的函式
給定陣列
int array[][4] = {
{ 1, 2, 3, 4},
{12, 13, 14, 5},
{11, 16, 15, 6},
{10, 9, 8, 7}
};
函式簽名可能是其中之一
// Declaring m as a Variable Length Array (since C99, optional from C11)
void spiral_traverse(int rows, int cols, int m[rows][cols]);
// or as a pointer to arrays of size 4
void spiral_traverse(int rows, int *m[4]);
把事情簡單化
該演算法的 C 實作與回圈的 exteme 存在問題,特別是第二個和第三個:
for ( indx_row = startRow 1; indx_row < endRow 1; indx_row )
// ^^^^^^^^^^^^
/* ... */ = *(*(vector indx_row) endCol);
for ( indx_col = endCol; indx_col > startCol; indx_col-- )
// ^^^^^^^^^^^^^^^^^
/* ... */ = *(*(vector endRow) indx_col);
第一個(嗯,第二個)回圈訪問的最后一個元素是vector[endRow][endCol],與另一個回圈首先訪問的元素相同。
以下版本產生預期的輸出。
請注意,我改變了回圈的極端。
#include <stdio.h>
void spiral_traverse(int rows, int cols, int m[rows][cols]);
int main(void)
{
int array[][4] = {
{ 1, 2, 3, 4},
{12, 13, 14, 5},
{11, 16, 15, 6},
{10, 9, 8, 7}
};
spiral_traverse(4, 4, array);
spiral_traverse(3, 4, array); // Elements are stored row-major.
}
void spiral_traverse(int rows, int cols, int m[rows][cols])
{ // m is a Variable Length Array ^^^^^^^^^^^^^^^^^
int startRow = 0;
int endRow = rows - 1;
int startCol = 0;
int endCol = cols - 1;
int new_array[rows*cols]; // <-- Another VLA
int i = 0;
while ((startRow <= endRow) && (startCol <= endCol))
{
for (int col = startCol; col < endCol; col, i)
{ // ^ ^^^^^
new_array[i] = m[startRow][col];
} // ^^^^^^^^^^^^^^^^
for (int row = startRow; row < endRow; row, i)
{ // ^^^^^^^^^^ ^ ^^^^^
new_array[i] = m[row][endCol];
} // ^^^^^^^^^^^^^^
for (int col = endCol; col > startCol; --col, i)
{ // ^^^^^^^^ ^ ^^^^^
new_array[i] = m[endRow][col];
} // ^^^^^^^^^^^^^^
for (int row = endRow; row > startRow; --row, i)
{ // ^^^^^^^^ ^ ^^^^^
new_array[i] = m[row][startCol];
} // ^^^^^^^^^^^^^^^^
startRow ;
endRow--;
startCol ;
endCol--;
}
puts("\nThe array below displays the elements in spiral order");
for (i=0;i<(rows*cols);i )
printf("%d, ", new_array[i]);
putchar('\n');
}
當然,如果目標只是列印元素,則不需要將它們全部復制到另一個陣列中。此外,引入函式指標引數,函式可以變得更加通用(參見例如此處)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/462804.html
標籤:C python-3.x 指针
