您的位置:首页 > 编程语言 > C语言/C++

基于Visual C++2013拆解世界五百强面试题--题3-打印螺旋数组

2013-10-08 22:17 507 查看
请用C语言实现 输入N,打印N*N矩阵

比如 N = 3, 打印:

1 2 3

8 9 4

7 6 5

N = 4, 打印

1 2 3 4

12 13 14 5

11 16 15 6

10 9 8 7

启动2012



输出结果



#include <stdio.h>
#include <stdlib.h>

#define M 5
int arr[M][M] = { 0 }; //初始化数组全0 ,用0来判断数组是否赋有正确的值

void HuiJu(void);   //矩阵赋值函数
void ShowArr(void); //输出矩阵

int main()
{
HuiJu();
ShowArr();

system("pause");
return 0;
}

void ShowArr(void)
{
int i = 0;
int j = 0;
for (i = 0; i < M; i++)
{
for (j = 0; j < M; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}

void HuiJu(void)
{
int i = 0; //数组索引
int j = 0; //数组索引
int direc = 1; //方向控制
int num = 1; //给数组进行赋值的变量

while (num <= M*M)//对矩阵循环赋值
{
switch (direc)
{
case 1:     //从左到右
while (arr[i][j] == 0 && j < M) //数组值为0以及索引j没有越界时进行
{
arr[i][j] = num;
num++;
j++; //i不变 j递增
}
j--; //改变索引为下一个变量的索引
i++; //改变索引为下一个变量的索引
direc = 2;//把方向从左到右改变为从上到下
break;

case 2:     //从上到下
while (arr[i][j] == 0 && i < M)//数组值为0以及索引i没有越界时进行
{
arr[i][j] = num;
num++;
i++; //j不变 i递增
}
i--; //改变索引为下一个变量的索引
j--; //改变索引为下一个变量的索引
direc = 3;//把方向从上到下改变为从右到左
break;

case 3:     //从右到左
while (arr[i][j] == 0 && j >= 0)//数组值为0以及索引j没有越界时进行
{
arr[i][j] = num;
num++;
j--; //i不变 j递减
}
j++; //改变索引为下一个变量的索引
i--; //改变索引为下一个变量的索引
direc = 4;//把方向从右到左改变为从上到上
break;

case 4:     //从下到上
while (arr[i][j] == 0) //从下到上只需要判断是否被正确赋值
{
arr[i][j] = num;
i--; //j不变 i递减
num++;
}
i++; //改变索引为下一个变量的索引
j++; //改变索引为下一个变量的索引
direc = 1; //把方向从下到上改变为从左到右
break;
}
}
}




源代码下载地址如下:

http://download.csdn.net/detail/yincheng01/6369295

网友评论中提出了递归的解决方法,认为还不错,下面我贴出他的解法和我的一种解法,大家有更好的解法也可以发到评论里.

网友的解法:

#include <iostream>
using namespace std;

int* sort(int n)
{
if (n < 1)
return NULL;
if (n == 1)
{
int *p = new int[1];
*p = 1;
return p;
}
else if (n == 2)
{
int *p = new int[4];
*p = 1;
*(p + 1) = 2;
*(p + 2) = 4;
*(p + 3) = 3;
return p;
}
else
{
int * iResult = new int[n*n];
int * iLast;
int i, j;
iLast = sort(n - 2);
for (i = 0; i < n - 1; i++)
*(iResult + i) = i + 1;
for (i = 0; i < n - 2; i++)
{
*(iResult + (i + 1)*n - 1) = n + i;
*(iResult + (i + 1)*n) = 4 * n - 4 - i;
for (j = 0; j < n - 2; j++)
{
*(iResult + (i + 1)*n + j + 1) = *(iLast + i*(n - 2) + j) + 4 * n - 4;
}
}
delete iLast;
*(iResult + n*(n - 1) - 1) = 2 * n - 2;
for (i = 0; i < n; i++)
*(iResult + n*(n - 1) + i) = 3 * n - 2 - i;
return iResult;
}
return NULL;
}
void main()
{
int i, n = 0;
while (n < 1)
{
printf("Please input a number which is bigger than 0:");
scanf("%d", &n);
}
int *pInt = sort(n);
for (i = 0; i < n*n; i++)
{
if (i%n == 0)
cout << endl;
cout << *(pInt + i) << "\t";
}
cout << endl;
delete pInt;
return;
}


当然解决的方法是非常多的,第一种方法是最好想到的,下面我贴出我的另一种方法供网友们参考。

#include <iostream>
using namespace std;

int* sort(int n)
{
int *p = new int[n*n];
*(p + (n / 2)*n + (n / 2)) = n*n;
int start = 1;
for (int i = 0; i < n / 2; i++)
{
for (int j = i; j < n - i - 1; j++)
{
*(p + i * n + j) = start;
*(p + j * n + n - i - 1) = start + n - 2 * i - 1;
*(p + (n - 1 - i)*n + n - j - 1) = start + 2 * (n - 1 - 2 * i);
*(p + (n - 1 - j)*n + i) = start++ + 3 * (n - 1 - 2 * i);
}
start += 3 * (n - 1 - 2 * i);
}
return p;
}

void main()
{
int i, n = 0;
while (n < 1)
{
printf("Please input a number which is bigger than 0:");
scanf("%d", &n);
}
int *pInt = sort(n);
for (i = 0; i < n*n; i++)
{
if (i%n == 0)
cout << endl;
cout << *(pInt + i) << "\t";
}
cout << endl;
delete pInt;
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: