基于Visual C++2013拆解世界五百强面试题--题3-打印螺旋数组
请用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;
}
基于Visual C++2013拆解世界五百强面试题--题3-打印螺旋数组的更多相关文章
- 基于Visual C++2013拆解世界五百强面试题--题2-输出和为一个给定整数的所有组合
请用C语言实现 输出和为一个给定整数的所有组合 启动2012 /* 请用C语言实现 输出和为一个给定整数的所有组合 */ #include <stdio.h> //包含头文件stdio.h ...
- 基于Visual C++2013拆解世界五百强面试题--题1-定义各种类型指针
用变量a给出下面的定义 a)一个整型数 b)一个指向整型数的指针 c)一个指向指针的指针,它指向的指针是指向一个整型数 d)一个有10个整型数的数组 e)一个有10个指针 ...
- 基于Visual C++2013拆解世界五百强面试题--题18-程序结果分析2-终结篇
第二部分程序结果分析,分析流程还是写入代码注释中 分析下面程序的输出: #include <stdio.h> int main() { char *a = "hello" ...
- 基于Visual C++2013拆解世界五百强面试题--题17-程序结果分析1
分析程序结果,分析过程我们就写在程序注释里面. 写出下列代码的输出内容 #include <stdio.h> int inc(int a) { return (++a); } int mu ...
- 基于Visual C++2013拆解世界五百强面试题--题16-进制分析
清写出下列代码的输出内容 #include <stdio.h> int main() { int a = -1, b = -12, c = -123, d = -1234; printf( ...
- 基于Visual C++2013拆解世界五百强面试题--题15-递归相加
有一分数序列: 1/2 , 1/4 , 1/6 , 1/8 ......,用递归的方法,求此数列20项之和. 可以看出规律:每一项位1/n*2 这个很容易些递归,但是要注意一点,使用浮点数相除保存: ...
- 基于Visual C++2013拆解世界五百强面试题--题14-循环删除
有一个数组a[1000]存放0-1000,要求每隔二个数删除一个数,到末尾时循环到开头继续进行,求最后一个被删掉数的原始下标. 看到题目可以用循环链表保存这些数,然后循环删除,大大减少了一些复杂的边界 ...
- 基于Visual C++2013拆解世界五百强面试题--题13-找最大公共子字符串
编程实现:找出两个字符串中最大公共子字符串,如"abccade"和"dgcadde"的最大子字符串为"cad". 如果不考虑效率的话直接比较 ...
- 基于Visual C++2013拆解世界五百强面试题--题12-进制转换
编程实现,把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列库函数. 转换成二进制,直接循环移位依次取每一位,判断1或0然后将相应字符放入字符串缓冲区中. 对于十六进制, ...
随机推荐
- Android中adb push和adb install的使用区别
Android中adb push和adb install的使用区别 转载 本篇文章由史迎春(@三俗小女子)投稿.转载请注明原文地址. 在Android实际开发中,经常会使用adb命令,安装应用程序可 ...
- SQL 递归 可以用于权限查找。迭代自身没有用递归函数。
昨晚看的书,发现可以用T-SQL 实现自定义递归,其实也就是变相的 foreach 直接上代码 ,不懂得可问: ),col int ) -- id 增量id pid该数据的上级增量id name 名称 ...
- POJ1323-Game Prediction
描述: Suppose there are M people, including you, playing a special card game. At the beginning, each p ...
- 转载[WampServer下使用多端口访问]
作者:韩子迟 原文链接:http://www.cnblogs.com/zichi/p/4589142.html 注意点:www和www2都需要安装服务: 在C:\wamp\bin\apache\Apa ...
- 每天一个小算法(4)----在O(1)时间删除指定结点
O(1)时间内删除结点的思路只能是复制该结点下一个结点的数据,然后删除该结点的下一个结点,来等效删除此结点. 需要注意的地方是删除头结点和尾结点的处理. #include <stdio.h> ...
- 自定义TWebBrowser浏览器控制遇到的一些问题
最近工作需要,要将TWebBrowser样式改头换面,包括菜单,滚动条等,都要换掉. 由于滚动条已经屏蔽掉,所以关于页面滚动等,全部都需要自己写代码来模拟了.模拟的过程中发现获得页面的客户区大小Cli ...
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)
[题目链接] http://www.spoj.pl/problems/PHRASES/ [题目大意] 求在每个字符串中出现至少两次的最长的子串 [题解] 注意到这么几个关键点:最长,至少两次,每个字符 ...
- c++实现将表达式转换为逆波兰表达式
https://github.com/Lanying0/lintcode 所属: 数据结构->线性结构->栈 问题: 给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号). ...
- How to get the xpath by clicking an html element
How to get the xpath by clicking an html element How to get the xpath by clicking an html element
- request.getParameterValues与request.getParameter的差别
一. 简单的对照 request.getParameter用的比較多,相对熟悉 request.getParameterValues(String name)是获得如checkbox类(名字同样, ...