清理磁盘空间的时候翻出了多年前写过的螺旋矩阵,代码效率和水平较低,纪念一下,保存到博客园!

// 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++ 螺旋矩阵算法的更多相关文章

  1. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  2. 【算法笔记】B1050 螺旋矩阵

    1050 螺旋矩阵 (25 分)   本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行  ...

  3. Java-基础编程(螺旋矩阵&乘法表)

    package cn.rick.study; import java.io.BufferedReader;import java.io.InputStreamReader;import java.ut ...

  4. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  5. 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 ...

  6. HrbustOJ 1564 螺旋矩阵

    Description 对于给定的一个数n,要你打印n*n的螺旋矩阵. 比如n=3时,输出: 1 2 3 8 9 4 7 6 5 Input 多组测试数据,每个测试数据包含一个整数n(1<=n& ...

  7. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

  8. 【NOIP2014 普及组】螺旋矩阵

    [NOIP2014 普及组]螺旋矩阵 一.题目 [NOIP2014 普及组]螺旋矩阵 时间限制: 1 Sec  内存限制: 128 MB 提交: 18  解决: 0 [提交][状态][讨论版] 题目描 ...

  9. 螺旋矩阵,两步进阶,从暴力到o(1)

    题目描述 一个 n 行 n 列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第 1 行第 1 列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵中所有格 ...

随机推荐

  1. Manjaro Linux 没有声音

    在Multimedia中的PulseAudio Volume Control中的设置可以解决

  2. ipmitool+python应用处理大量带外地址

    ipmitool 是一种可用在 linux 系统下的命令行方式的 ipmi 平台管理工具,它支持 ipmi 1.5 规范(最新的规范为 ipmi 2.0),通过它可以实现获取传感器的信息.显示系统日志 ...

  3. PAT 甲级 1105 Spiral Matrix

    https://pintia.cn/problem-sets/994805342720868352/problems/994805363117768704 This time your job is ...

  4. CentOS75 安装Oracle18c

    1. 参考地址 https://blog.csdn.net/u010257584/article/details/50902472https://www.cnblogs.com/kerrycode/a ...

  5. PSP(4.6——4.12)以及周记录

    1.PSP 4.6 8:30 10:30 20 100 博客 B Y min 12:00 13:00 5 55 Account A Y min 13:05 13:15 0 10 站立会议 A Y mi ...

  6. obj.getClass() == Person.class 用于判断类型

    obj.getClass() == Person.class  用于判断类型

  7. vue中的minix

    minix 是个什么东西, 就是混合,把你混合给我 浅显表述就是 你说 : ‘我叫李四’, 我说 : ‘我叫张三’, 然后把你 混合给我, 就成了 我说 : ‘我叫张三我叫李四’, 所有解说都在例子里 ...

  8. Django_重装系统后无法使用 sqlite 数据库报错:com.intellij.execution.ExecutionException: Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC

     重装系统后无法使用 sqlite 数据库报错 报错 : com.intellij.execution.ExecutionException: Exception in thread "ma ...

  9. python里使用正则表达式的非贪婪模式

    在正则表达式里,什么是正则表达式的贪婪与非贪婪匹配 如:String str="abcaxc"; Patter p="ab*c"; 贪婪匹配:正则表达式一般趋向 ...

  10. Eclipse Neon 汉化

    官网下载的Eclipse是英文版的,对于初学者来说为了减小学习的难度,将英文版汉化是有必要的. 第一步:依次点击Eclipse菜单栏上的“Help"-->”About",查看 ...