一个数组打印的方法,请高手们帮实现

ladofwind 2015-03-02 03:48:13
输入:
5 3
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
输出:
1 2 3 4 5 9 13 12 11 6 7 8

输出规则:
先向右-》左下-》左-》上
第一行是metrics的size,和行数, 第二行开始是数据,实现下面方法,main方法能够正确输出

================================================================
void convertMatrix(int width, int height, int* matrix, int *result_size, int** result) {
}
===============================================================
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);
int res_i;
for(res_i=0; res_i < res_size; res_i++) {

printf("%d ", res[res_i]);

}


return 0;
}
...全文
172 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiht594 2015-03-03
  • 打赏
  • 举报
回复
#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;
}
赵4老师 2015-03-02
  • 打赏
  • 举报
回复
仅供参考:
#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");
    }
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧