user-0 提问: 一个数组打印的方法，请高手们帮实现

回复:
user: user-1
content: #include <stdafx.h> #include <stdio.h> #include <Windows.h> #include <stdlib.h> #include <memory.h>  bool sprintRight(int *pSrc, 	int *pTarget, 	const int width, 	/*const int height,*/ 	unsigned ¤tRow, 	unsigned ¤tCol, 	unsigned ¤tSize, 	const int indicator) { 	bool bRet = true; 	for (int iCol = currentCol; iCol <= width; ++iCol) 	{ 		unsigned pos = (currentRow - 1)* width + iCol -1; 		if(pSrc[pos] != indicator) 		{ 			pTarget[currentSize] = pSrc[pos]; 			pSrc[pos] = indicator; 			printf("%d, ", pTarget[currentSize]); 			currentSize++; 			currentCol++; 		} 		else 		{ 			bRet = false; 			break; 		} 	}  	currentCol -= 2; 	currentRow++; 	return bRet; }  bool sprintLeftDown(int *pSrc, 	int *pTarget, 	const int width, 	const int height, 	unsigned ¤tRow, 	unsigned ¤tCol, 	unsigned ¤tSize, 	const int indicator) { 	bool bRet = true; 	while (currentRow <= height) 	{ 		unsigned pos = (currentRow-1) * width + currentCol-1; 		if (pSrc[pos] != indicator) 		{ 			pTarget[currentSize] = pSrc[pos]; 			pSrc[pos] = indicator; 			printf("%d, ", pTarget[currentSize]); 			currentSize++;  			currentRow++; 			currentCol--; 		} 		else 		{ 			bRet = false; 			break; 		} 	} 	currentRow--; 	return bRet; }  bool sprintLeft(int *pSrc, 	int *pTarget, 	const int width, 	/*const int height,*/ 	unsigned ¤tRow, 	unsigned ¤tCol, 	unsigned ¤tSize, 	const int indicator) { 	bool bRet = true; 	while (currentCol > 0) 	{ 		unsigned pos = (currentRow-1) * width + currentCol-1; 		if (pSrc[pos] != indicator) 		{ 			pTarget[currentSize] = pSrc[pos]; 			pSrc[pos] = indicator; 			printf("%d, ", pTarget[currentSize]); 			currentSize++; 			currentCol--; 		} 		else 		{ 			bRet = false; 			break; 		} 	} 	currentRow--;// for sprintTop 	currentCol++; 	return bRet; } bool sprintTop(int *pSrc, 	int *pTarget, 	const int width, 	unsigned ¤tRow, 	unsigned ¤tCol, 	unsigned ¤tSize, 	const int indicator) { 	bool bRet = true; 	while (currentRow > 0) 	{ 		unsigned pos = (currentRow-1) * width + currentCol-1; 		if (pSrc[pos] != indicator) 		{ 			pTarget[currentSize] = pSrc[pos]; 			pSrc[pos] = indicator; 			printf("%d, ", pTarget[currentSize]); 			currentSize++; 			currentRow--; 		} 		else 		{ 			bRet = false;  			break; 		} 	} 	currentCol++; 	currentRow++; 	return bRet; } void  convertMatrix(int width, int height, int* matrix, int *result_size, int** result) { 	size_t totalsize = width * height; 	int *pTarget = (int *)malloc(totalsize * sizeof(int)); 	* result = pTarget; 	int indicator = 0;//present the invalid value 	for (size_t i = 0; i < totalsize; ++i) 	{ 		pTarget[i] = indicator; 	}  	unsigned currentSize = 0;//of pTarget 	unsigned currentRow = 1;//of matrix 	unsigned currentCol = 1;//of matrix 	bool bRet1 = true, bRet2 = true, bRet3 = true, bRet4 = true; 	bool bGoOn = true; 	while ((currentSize < totalsize) 		&& bGoOn) 	{ 		//bRet1234: reach a border or not 		bRet1 = sprintRight(matrix, pTarget, width,currentRow, currentCol, currentSize, indicator); 		bRet2 = sprintLeftDown(matrix, pTarget, width, height,currentRow, currentCol, currentSize, indicator); 		bRet3 = sprintLeft(matrix, pTarget, width, currentRow, currentCol, currentSize, indicator); 		bRet4 = sprintTop(matrix, pTarget, width, currentRow, currentCol, currentSize, indicator); 		bGoOn = bRet1 || bRet2 || bRet3 || bRet4; 	}  	*result_size = currentSize; } int main() { 	int res_size; 	int* res; 	int _width; 	scanf("%d", &_width);  	int _height; 	scanf("%d", &_height);  	int *_matrix = (int*) malloc(_width * _height * sizeof(int)); 	int _matrix_i; 	for(_matrix_i = 0; _matrix_i < _width * _height; _matrix_i++) 	{ 		int _matrix_item; 		scanf("%d", &_matrix_item);  		_matrix[_matrix_i] = _matrix_item; 	}  	convertMatrix(_width, _height, _matrix, &res_size, &res); 	printf("\n"); 	int res_i; 	for(res_i=0; res_i < res_size; res_i++) 	{  		printf("%d ", res[res_i]);  	} 	free(_matrix); 	free(res); 	system("pause"); 	return 0; }

user: user-2
content: 仅供参考：#include <stdio.h> #define MAXN 100 int m[MAXN+2][MAXN+2]; char d; int x,y,k,n,w; char str[10]; void main() {     while (1) {         printf("Input n(1..%d):",MAXN);         fflush(stdout);         rewind(stdin);         if (1==scanf("%d",&n)) {             if (1<=n && n<=MAXN) break;         }     }     y=0  ;for (x=0;x<=n+1;x++) m[y][x]=1;     y=n+1;for (x=0;x<=n+1;x++) m[y][x]=1;     x=0  ;for (y=0;y<=n+1;y++) m[y][x]=1;     x=n+1;for (y=0;y<=n+1;y++) m[y][x]=1;     for (y=1;y<=n;y++) {         for (x=1;x<=n;x++) {             m[y][x]=0;         }     }     x=1;     y=1;     k=0;     d='D';     while (1) {         k++;         if (k>n*n) break;         m[y][x]=k;         switch (d) {             case 'D':                 if (0==m[y+1][x])  y++;                 else              {x++;d='R';}             break;             case 'R':                 if (0==m[y][x+1])  x++;                 else              {y--;d='U';}             break;             case 'U':                 if (0==m[y-1][x])  y--;                 else              {x--;d='L';}             break;             case 'L':                 if (0==m[y][x-1])  x--;                 else              {y++;d='D';}             break;         }     }     w=sprintf(str,"%d",n*n);     for (y=1;y<=n;y++) {         for (x=1;x<=n;x++) {             printf(" %0*d",w,m[y][x]);         }         printf("\n");     } }