C++ 螺旋矩阵算法
清理磁盘空间的时候翻出了多年前写过的螺旋矩阵,代码效率和水平较低,纪念一下,保存到博客园!
// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std; ///////////螺旋矩阵,从外往里/////////// class Point
{
public:
int x;
int y;
Point()
{
x=;
y=;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
loop:
int width=,state=,Number=;
std::cout<<"请输入宽度:";
cin>>width; Point LeftTop,RightTop,RightBottom,LeftBottom;
RightTop.x=width;
RightBottom.x=width;
RightBottom.y=width;
LeftBottom.y=width; //初始化数组
int **arr=new int *[width];
for(int len=;len<width;arr[len++]=new int[width]()); while (Number<=(width*width))
{
if (==state)
{
for (int i = LeftTop.x; i < RightTop.x ; i++)
{
arr[LeftTop.y][i]=Number++;
}
LeftTop.y=++RightTop.y;
state=;
continue;
}
if (==state)
{
for (int i = RightTop.y; i < RightBottom.y; i++)
{
arr[i][RightTop.x-]=Number++;
}
RightTop.x=--RightBottom.x;
state=;
continue;
}
if (==state)
{
for (int i = RightBottom.x-; i >= LeftBottom.x; i--)
{
arr[LeftBottom.y-][i]=Number++;
}
RightBottom.y=--LeftBottom.y;
state=;
continue;
}
if (==state)
{
for (int i = LeftBottom.y-; i >= LeftTop.y; i--)
{
arr[i][LeftTop.x]=Number++;
}
LeftTop.x=++LeftBottom.x;
state=;
continue;
}
} std::cout<<endl; //输出
for(int x=;x<width;x++)
{
for(int y=;y<width;y++)
{
std::cout<<setw()<<arr[x][y];
}
std::cout<<endl<<endl;
}
std::cout<<endl<<endl;
goto loop;
return ;
}
C++ 螺旋矩阵算法的更多相关文章
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
- 【算法笔记】B1050 螺旋矩阵
1050 螺旋矩阵 (25 分) 本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行 ...
- Java-基础编程(螺旋矩阵&乘法表)
package cn.rick.study; import java.io.BufferedReader;import java.io.InputStreamReader;import java.ut ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode 54. Spiral Matrix(螺旋矩阵)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- HrbustOJ 1564 螺旋矩阵
Description 对于给定的一个数n,要你打印n*n的螺旋矩阵. 比如n=3时,输出: 1 2 3 8 9 4 7 6 5 Input 多组测试数据,每个测试数据包含一个整数n(1<=n& ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
- 【NOIP2014 普及组】螺旋矩阵
[NOIP2014 普及组]螺旋矩阵 一.题目 [NOIP2014 普及组]螺旋矩阵 时间限制: 1 Sec 内存限制: 128 MB 提交: 18 解决: 0 [提交][状态][讨论版] 题目描 ...
- 螺旋矩阵,两步进阶,从暴力到o(1)
题目描述 一个 n 行 n 列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第 1 行第 1 列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵中所有格 ...
随机推荐
- Java 笔记——在 IDEA 中使用 Maven 配置和使用 MyBatis
1.前言 MyBatis 是什么? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射. MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. ...
- Arduino下读取DHT22温湿度(不使用第三方库)
代码如下: #include <inttypes.h> /* * LED */ unsigned ; /* * DHT22配置程序 */ unsigned ; #define DHT_OK ...
- ELK Stack (2) —— ELK + Redis收集Nginx日志
ELK Stack (2) -- ELK + Redis收集Nginx日志 摘要 使用Elasticsearch.Logstash.Kibana与Redis(作为缓冲区)对Nginx日志进行收集 版本 ...
- MySQL中EXPLAIN解释命令 查看索引是否生效
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...
- [转帖]Docker里运行Docker docker in docker(dind)
Docker里运行Docker docker in docker(dind) http://www.wantchalk.com/c/devops/docker/2017/05/24/docker-in ...
- PHP阿里云云解析签名, 通过API 绑定域名到动态 ip
<?php date_default_timezone_set("GMT"); //绑定 ip 到域名 Ali::Obj()->UpdateDomainRecord() ...
- sessionStorage & string typeof
sessionStorage & string typeof
- python自动化之爬虫原理及简单案例
[爬虫案例]动态地图里的数据如何抓取:以全国PPP综合信息平台网站为例 http://mp.weixin.qq.com/s/BXWTf5hmq8vp91ZvgaphEw [爬虫案例]动态页面的抓取! ...
- 【树状数组套主席树】带修改区间K大数
P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ...
- 【洛谷P3916】图的遍历
题目大意:给定一个 N 个点,M 条边的有向图,求每个点能够到达的节点的最大编号是多少. 题解:因为题中所给图不一定是一个 DAG,因此无法进行按照拓扑序来动态规划,需要另辟蹊径.由于求的是每个节点能 ...