【矩阵】蛇形 & 平方

平方矩阵(ⅠⅡ Ⅲ)C++ 【代码】平方矩阵(ⅠⅡ Ⅲ)C++ 阅读详情

二维矩阵,在游戏开发中,有很多的应用,更多时候,也需要一点向量的知识。本文主要写 蛇形矩阵 的认识和题解,配点简单的 平方矩阵 内容。

蛇形矩阵

输入两个整数 nnn 和 m(1≤n,m≤100)m(1≤n,m≤100)m(1≤n,m≤100),输出一个 nnn 行 mmm 列的二维矩阵,将数字 111 到 n×mn×mn×m 按照回字蛇形填充至矩阵中。

在这里插入图片描述

输入格式

输入共一行,包含两个整数 n 和 m。

输出格式

输出满足要求的矩阵。

矩阵占 nnn 行,每行包含 mmm 个空格隔开的整数。

输入输出样例

输入样例1

3 3

输出样例1

1 2 3
8 9 4
7 6 5

样例解释:无。

题解 1

思路:

  1. 点坐标为 (x,y)(x, y)(x,y),开始后,沿着 右→下→左→上 顺时针移动。
  2. 依靠偏移量来实现点的移动:dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}
  3. d 来确定如何偏移和顺时针走圈,d 实现 右→下→左→上 时,取值为 1→2→3→01→2→3→01→2→3→0 :d = (d + 1) % 4
  4. 下一个位置 (a,b)(a, b)(a,b) 撞墙或越界时,需要转向:a < 0 || a >= n || b < 0 || b >= m || q[a][b] != 0

点的顺时针走向

#include <iostream>
using namespace std;

const int N = 110;

int n, m;
int q[N][N];    // main 外声明数组,元素赋 0

int main()
{
    cin >> n >> m;
    
    // 记录坐标(下标)的偏移量
    int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
    int x = 0, y = 0;  // 左上角开始向右走
    int d = 1;  // 1 右 2 下 3 左 0 上
    for (int i = 1; i <= n*m; i ++) {
        // 左上角 (0, 0) 赋 1
        q[x][y] = i;
        // 开始向右走
        int a = x + dx[d], b = y + dy[d];
        // 判断是否 越界或撞墙,若是,则顺时针转向
        if(a < 0 || a >= n || b < 0 || b >=m || q[a][b] != 0) {
            d = (d + 1) % 4;    // 圈
            a = x + dx[d], b = y + dy[d];   // 转向
        }
        x = a, y= b;
    }
    
    
    for (int i = 0; i < n; i ++ ) {
        for (int j = 0; j < m; j ++ ) {
            cout << q[i][j] << " ";
        }
        cout << endl;
    }
    
    return 0;
}

平方矩阵

输入整数 N(0≤N≤100)N(0≤N≤100)N(0≤N≤100),输出一个 NNN 阶的二维数组。

数组的形式参照样例。

输入格式

输入包含多行,每行包含一个整数 NNN。

当输入行为 N=0N=0N=0 时,表示输入结束,且该行无需作任何处理。

输出格式

对于每个输入整数 NNN,输出一个满足要求的 NNN 阶二维数组。

每个数组占 NNN 行,每行包含 NNN 个用空格隔开的整数。

每个数组输出完毕后,输出一个空行。

输入输出样例

输入样例1

1
2
3
4
5
0

输出样例

1

1 2
2 1

1 2 3
2 1 2
3 2 1

1 2 3 4
2 1 2 3
3 2 1 2
4 3 2 1

1 2 3 4 5
2 1 2 3 4
3 2 1 2 3
4 3 2 1 2
5 4 3 2 1

题解 1

思路:左上到右下的对角线,对角线上每个点向下、向右延申加 1。

#include <iostream>
#include <cmath>
using namespace std;

const int N = 110;

int n;
int a[N][N];

int main()
{
    while(cin >> n, n) {

        for(int i = 1; i <= n; i++) {
            for(int j = i, k = 1; j <= n; j++, k++) {
                a[i][j] = k;    // i 不变(行不动),随着 j ++,k ++ 向右延伸
                a[j][i] = k;    // i 不变(列不动),随着 j ++,k ++ 向下延伸
            }
        }
		// 输出
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }    

   return 0;
}

题解 2

思路:沿着对角线,将内容分成三个部分,对角线左侧、对角线、对角线右侧

#include <iostream>
#include <cmath>

using namespace std;

const int N = 110;

int n;
int a[N][N];

int main()
{
    while(cin >> n, n) {

        for(int i = 1; i <= n; i++) {
            for(int j = 1, k = i; j <= n; j++) {
                if(j < i) a[i][j] = k --;
                if(j == i) a[i][j] = k = 1;
                if(j > i) a[i][j] = ++ k;
            }
        }

        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }    

   return 0;
}

思路:或沿着对角线分成两个部分:[i...1],[2...n - i + 1]

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n;
    while(cin >> n, n) {

        for(int i = 1; i <= n; i++) {
            for(int j = i; j >= 1; j--) cout << j << " ";
            for(int j = i + 1; j <= n; j++) cout << j - i + 1 << " ";
            cout << endl;
        }
        cout << endl;
    }    

   return 0;
}

题解 3

思路:每一个位置上的值,都等于行列差的绝对值 + 1:∣row−col∣+1| row - col | + 1∣row−col∣+1。

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n;
    while(cin >> n, n) {

        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                cout << abs(i - j) + 1 << " ";
            }
            cout << endl;
        }
        cout << endl;
    }    

   return 0;
}
平方矩阵 II--C++ 经典解析版-----编程/算法/数据结构 题目描述 输入整数 N,输出一个 N 阶的二维数组。 数组的形式参照样例。 题目来源 平方矩阵II 输入格式 输入包含多行,每行包含一个整数 N。 当输入行为 N=0 时,表示输入结束,且该行无需作任何处理。 输出格式 对于每个输入整数 N,输出一个满足要求的 N 阶二维数组。 每个数组占 N 行,每行包含 N 个用空格隔开的整数。 每个数组输出完毕后,输出一个空行。 数据范围 0≤N≤100 输入样例 1 2 3 4 5 0 输出样例 1 1 2 2 1 1 2 3 2 1 2 3 2 1 1 2 阅读详情

相关推荐

[蓝桥杯2020] C.蛇形填数 [蓝桥杯2020] E.成绩统计 小平方 卡片 城邦

如下图所示,小明用从1 开始的正整数“蛇形”填充无限大的矩阵。1 2 6 7 15 ...3 5 8 14 ...4 9 13 ...10 12 ...11 ...容易看出矩阵第二行第二列中的数是5。请你计算矩阵中第20 行第20 列的数是多少?无这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只输出这个整数,输出多余的内容将无法得分。一道简单的模拟题,没什么好讲的 二,成绩统计小蓝给学生们组织了一场考试,卷面总分为100 分,每个学生的得分都是一个0 到100 的整数

m0_62945899的博客 373

平方矩阵||

简单语法练习-二维数组5

m0_74012728的博客 384

面向网络攻击行为可视化分析系统实现【WebSocket + 可视化】

本系统主要应用于网站运维管理,它解决了网站运维人员排查网络攻击行为复杂且繁琐等问题,提高了运维人员网络安全分析的效率和准确性。

oNew_Lifeo的博客 1621

C语言/C++ 平方矩阵 数学最小值解法【简单易懂,代码可以直接运行】

C语言/C++ 平方矩阵【简单易懂,代码可以直接运行】 首先先解释一些什么是回环矩阵: 如图所示即是回环矩阵,也是这篇文章里面这道题的解法 输入整数 N,输出一个 N 阶的回字形二维数组。 数组的最外层为 1,次外层为 2,以此类推。 输入格式 输入包含多行,每行包含一个整数 N。 当输入行为 N=0 时,表示输入结束,且该行无需作任何处理。 输出格式 对于每个输入整数 N,输出一个满足要求的 N 阶二维数组。 每个数组占 N 行,每行包含 N 个用空格隔开的整数。 每个数组输出完毕后,输出一个空行。 数

于笨笨的博客 1711

【理论&实战篇】大模型RAG技术详解(一)-基础篇

RAG的初衷是为了增强大模型的事实性、时效性,减少幻觉,引入专业知识优化生成回复,是一种结合了信息检索技术与语言生成模型的人工智能技术。通过,并将其作为提示(Prompt)输入给大模型(LLM),来。

qq_41375680的博客 374

DSP配置I2C通讯(非IO口软件模拟时序)

dsp即数字信号处理器,是一种广泛应用的嵌入式处理器,主要应用是实时快速地实现各种数字信号处理算法,目前,国际主要的dsp供应商是ti公司,其tms32系列产品占据了dsp市场近一半的份额,本文以TMS320F28377D为例,使用ti公司dsp开发工具ccs10.1配置i2c模块。 写这篇文章的缘由,因为市面上很多DSP其实内部是具有硬件I2C接口的,但由于网络上大部分资料和例程都是利用IO口模拟I2C时序进而实现I2C通讯的。给很多想学习硬件I2C通讯的小伙伴带来了困难。 下面先介绍一下配置步骤 1.E

weixin_44387442的博客 5546

输出平方矩阵

对于每一个点,其数据应当是其到四条边距离的最小值 即value=min(min(i,j),min(n-i+1,n-j+1))(+1算上自己本身的距离) #include&lt;iostream&gt; #include&lt;cmath&gt; using namespace std; int main(){ while(1){ i...

qq_41320429的博客 392

sift、surf、orb 特征提取——三维重建

参考文章:[sift、surf、orb 特征提取及最优特征点匹配] 最近刚刚入门三维重建,在SFM算法的代码中注意到了feature_type( feature extraction method)这个参数,想细致了解一下这几种特征提取算法,在这里做一下笔记。 SIFT 简介 SIFT(Scale-Invariant Feature Transform)——尺度不变特征变换,是一种计算机视觉的特征提取算法,用来侦测与描述图像中的局部特征。 实质上,它是在不同的尺度空间上查找关键点(特征点),并计算出关键点的

bb_sy_w的博客 4362

每日算法刷题Day5-平方矩阵II和III、蛇形矩阵图解

⭐每日算法题解系列文章旨在精选重点与易错的算法题,总结常见的算法思路与可能出现的错误,与笔者另一系列文章有所区别,并不是以知识点的形式提升算法能力,而是以实战习题的形式理解算法,使用算法。🔥本文已收录于算法刷题系列专栏:欢迎订阅,持续更新。

timerring的博客 1万+

详细Ubuntu 22.04 配置最新UR机械臂仿真环境(ros2-humble, ur-sim, ur-ros2-driver)

使用ubuntu22.04.01和ur-sim, ur-driver进行ur机械臂的仿真

verloge的博客 1万+

uniapp [安卓苹果App端] - 在“安卓app/苹果app“平台端打包编译后,微信授权登录报错 login:fail [:-1]未能完成操作。(PGWXAPI错误-1。)错误码code:-10

uni-app,安卓Android,苹果ios,安卓app端,苹果App端,uniapp苹果安卓app,微信授权登录,uniAppAPP端微信登录报错,出现微信确认登录界面后点击登录报错,BundleID,应用ID,微信登录,app微信一键登录,微信登录绑定点确定就出错,AppSecrect,login:fail[:-1]未能完成操作。(PGWXAPI错误-1。),"errCode":-100,"code":-100,在uniappmanifest.json配置了AppSecrect还是报错无

🏆 前端领域优质创作者 3764

平方矩阵 I、蛇形填数(C++)

平方矩阵题目 输入整数N,输出一个N阶的回字形二维数组。 数组的最外层为1,次外层为2,以此类推。 输入格式 输入包含多行,每行包含一个整数N。 当输入行为N=0时,表示输入结束,且该行无需作任何处理。 输出格式 对于每个输入整数N,输出一个满足要求的N阶二维数组。 每个数组占N行,每行包含N个用空格隔开的整数。 每个数组输出完毕后,输出一个空行。 数据范围 0≤N≤100 输入样例...

大锤 900

华为OD新系统机试真题-勇攀数字高峰(Java/Py/C/C++/Js/Go)

新系统 华为OD机试新系统【勇登数字高峰】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD新系统机试真题【勇登数字高峰】

qq_45776114的博客 1549

【平方矩阵 + 蛇形矩阵】

【代码】【平方矩阵找规律】

三分梦的博客 546

YOLO11涨点优化:创新多尺度卷积注意力(MSCA)模块,小目标检测神器!

MSCA模块通过创新的多尺度设计,在保持计算效率的同时显著提升小目标检测能力。实验证明,在YOLOv5/v8等模型上嵌入MSCA后,mAP提升超过2%,小目标检测精度提升近4%,且计算代价增加极小。注意力机制的未来发展方向动态核尺寸:根据输入特征自适应调整卷积核大小跨模态融合:结合RGB与深度/热力图等多模态信息自监督优化:利用对比学习预训练注意力模块尝试在你的YOLO模型中插入MSCA模块,欢迎在评论区分享你的涨点结果!参考文献。

QQ_778132974的博客 470

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

java算法001:魔方矩阵,蛇形矩阵,螺旋矩阵

题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数。

weixin_44699728的博客 944

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = &quot;R. B. Palm&quot;, title = &quot;Prediction as a candidate for learning deep hierarchical models of data&quot;, year = &quot;2012&quot;,}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er&lt;0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free &quot;Bitdeli Badge&quot;)

平方矩阵()

【代码】平方矩阵。

m0_63925226的博客 863

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

算法篇-蛇形矩阵

题目:在n*n方阵里填入1,2,3,···,n*n,要求填成蛇形,例如n=4时方阵为:                                                        10 11 12 1                                                        9   16 13 2                     

想去海邊 5726

平方矩阵问题

在这个程序中,每个位置 ( 𝑖 , 𝑗 )需要填充一个数,该数的计算方式是取该位置到四条边的最短距离。

m0_73997331的博客 597

蛇形矩阵算法思路

//规律:从中心开始,移动的顺序为右、上、左、下 //从中心点a[n/2][n/2]移动的次数分别为向右1次,向上1次,向左2次,向下2次,向右3次,向上3次,向左4次,向下4次等等循环下去。 //循环终止的条件为count==n #include &amp;amp;amp;amp;lt;iostream&amp;amp;amp;amp;gt; using namespace std; int number=1; int step=1; int Way(int...

qq_42590192的博客 3567

753. 平方矩阵 I、754. 平方矩阵 II、755. 平方矩阵 III

747. 数组的左上半部分 #include &lt;iostream&gt; #include&lt;stdio.h&gt; using namespace std; int main() { double a[12][12]; char c; cin&gt;&gt;c; for(int i=0;i&lt;12;i++) { for(int j=0;j&lt;12;j++) { cin&gt;&gt;a[i][j]; } }

LEO的博客 297

蛇形(回形)矩阵

最近面试又碰到了蛇形矩阵的输出,算是比较基础的算法实现,但以为时间紧迫思路有点乱做的不太完美感觉有些东西还是得动手记录一下才有助于记忆和理解,以后碰到了想不起来也可以翻来看看,所以还是得坚持写一下博客。 问题描述: 根据输入的树n,输出其回形矩阵,实例如下: 输入: 4 输出: 1 2 3 4 12 13 14 5 ...

IVON 2140

C++数组——矩阵相关题目

矩阵相关问题(平方矩阵I~III、蛇形矩阵) 文章目录矩阵相关问题(平方矩阵I~III、蛇形矩阵)一、平方矩阵I二、平方矩阵II三、平方矩阵III四、蛇形矩阵 或者称 回型矩阵 一、平方矩阵I 平方矩阵I题目详情 输入样例: 1 2 3 4 5 0 输出样例: 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 2 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 2 3 2 1 1 2 2 2 1 1 1 1 1 1 思

qq_51340322的博客 1723

【2022.12.10每天四个简单题之简单题大智慧系列4】(平方矩阵 I ,平方矩阵 II ,平方矩阵 III , 蛇形矩阵 )

【2022.12.10每天四个简单题之简单题大智慧系列4】(平方矩阵 I ,平方矩阵 II ,平方矩阵 III , 蛇形矩阵 )

m0_74018560的博客 402

【蛇形方阵(二维数组)】

根据题目意思,我们可以创建一个二维数组用来存储方阵,还知道数字填入方阵的顺序:向右→向下→向左→向上,以此为一个循环,循环下去。那么现在就出现两个问题:一,什么时候改变存储方向,二,循环到那停止。给出一个不大于 9 的正整数 n,输出 n×n的蛇形方阵。从左上角填上 从1 开始,顺时针方向依次填入数字,如同样例所示。一,我们可以给二维数组初始化一个相同的随机值,打上标记,有标记的地方证明没有被赋值,二 当输入的数等于方阵的最大数时停止,也就是n的平方。输入一个正整数 n,含义如题所述。

weixin_74917468的博客 1267

754. 平方矩阵 II ( 模拟 )

AcWing:754. 平方矩阵 II 可以使用类似于打印蛇形数组的方式、 756. 蛇形矩阵 ( 模拟 ) 模拟即可 AC Code import java.util.*; import static java.lang.System.out; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in);

lankerens 508
上一篇: 【PAT乙级】题解-1056 组合数的和
下一篇: 【JavaSE基础】06-集合框架(一)
老坛算粉
博客等级 码龄12年 708粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老坛算粉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值