一种打印螺旋式矩阵的方法
2015-08-04 10:58
351 查看
用户输入一个维度,打印从中心向外逆时针旋转的矩阵,左上角为最后一个数。例如输入5,输出为:
24 23 22 21 20
09 08 07 06 19
10 01 00 05 18
11 02 03 04 17
12 13 14 15 16
24 23 22 21 20
09 08 07 06 19
10 01 00 05 18
11 02 03 04 17
12 13 14 15 16
#include "stdafx.h" #include "stdlib.h" int _tmain(int argc, _TCHAR* argv[]) { int n = 0; long n_max = 0; int x_h,x_l,y_h,y_l; int i = 0; int j = 0; printf("请输入数字:"); scanf("%d",&n); int **CircleArray; CircleArray = new int * ; for (int t = 0; t < n; t++) { CircleArray[t] = new int ; } n_max = n*n - 1; x_h = n-1; y_h = n-1; x_l = 0; y_l = 0; while(n_max) { i = x_l; for (j = y_l; j <= y_h; j++)//上边 { if (0 == n_max) { CircleArray[i][j] = 0; break; } else { CircleArray[i][j] = n_max--; } } x_l += 1; j = y_h; for (i = x_l; i <= x_h; i++)//右边 { if (0 == n_max) { CircleArray[i][j] = 0; break; } else { CircleArray[i][j] = n_max--; } } y_h -= 1; i = x_h; for (j = y_h; j >= y_l; j--)//下边 { if (0 == n_max) { CircleArray[i][j] = 0; break; } else { CircleArray[i][j] = n_max--; } } x_h -= 1; j = y_l; for (i = x_h; i >= x_l; i--)//左边 { if (0 == n_max) { CircleArray[i][j] = 0; break; } else { CircleArray[i][j] = n_max--; } if (0 == n_max)//零值出现在最后,特殊处理 { i = x_l; j = y_l + 1; CircleArray[i][j] = 0; } } y_l += 1; } for (int p = 0; p < n; p++) //打印数组 { for (int q = 0; q < n; q++) { printf("%4d ",CircleArray[p][q]); } printf("\n"); } system("pause"); return 0; }
相关文章推荐
- 欢迎使用CSDN-markdown编辑器
- hadoop2.6伪分布+pig0.15+zookeeper3.4.6安装
- Android仿qq聊天记录长按删除功能效果
- subversion SVN
- [hdu1028]整数拆分,生成函数
- HTML5 FormData 用jquery 异步上传报错
- Android仿qq聊天记录长按删除功能效果
- php面向对象(OOP)编程完整教程
- iOS-UIkit复习和代理的使用实现文本框限制输入字数控制
- Eclipse全屏调试时,如何屏蔽控制台console弹出骚扰!!!
- oracle case where 复杂sql语句
- 简单记录平时常用容易混淆的小知识点
- iOS UTF-8、URL解码与转码
- 代码规范
- Java的日期转化 和 计算
- 南邮 OJ 1092 圆柱体的表面积
- 容器简介
- 关于前置++和后置++的那么点纠结
- 光标属性cursor
- 此集合已经采用方案 http 的地址。此集合中每个方案中最多只能包含一个地址。