N * N螺旋矩阵(以5*5螺旋矩阵为例)

news/2024/10/21 9:12:07/

N * N螺旋矩阵(以5*5螺旋矩阵为例)

代码如下:

public class NnMatrix {public static void main(String[] args) {test.matrix(5);//直接调用方法}
}class test{public static void matrix(int n) {int arr[][] = new int[n][n];//定义一个二维数组int count = 1;//记录数值int left = 0;//左int right = n - 1;//右int top = 0;//上int bottom = n - 1;//下while (count <= n*n){for (int i = left;i<=right;i++){//从左到右arr[top][i] = count++;}top++;for (int i = top;i<=bottom;i++){//从上到下arr[i][right] = count++;}right--;for (int i = right;i>= left;i--){//从右到左arr[bottom][i] = count++;}bottom--;for (int i = bottom;i>= top;i-- ){//从下到上arr[i][left] = count++;}left++;}//输出for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){System.out.print(arr[i][j] + " ");}System.out.println();}}
}

http://www.ppmy.cn/news/461292.html

相关文章

螺旋矩阵(正螺旋和逆螺旋)

package com.classmassage.cn.util;public class Lxjz {/*** question: 给你一个正整数 n &#xff0c;生成一个包含 1 到 n2 所有元素&#xff0c;且* 元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。*/public static void main(String[] args) {printMatrix(generate…

字蛛使用方法

先安装node.js 如果没有先下载&#xff1a;https://nodejs.org/en/download/ 打开Node.js command prompt如下图所示&#xff0c;并输入命令&#xff1a;npm install font-spider -g 回车执行 切换到你写的css页面声明字体&#xff0c;方法与CSS3的font-face方法类似 font-face…

狼与狮子的选择

狼与狮子的选择 上帝把两群羊放在草原上&#xff0c;一群在南&#xff0c;一群在北。上帝还给羊群找了两种天敌&#xff0c;一种是狮子&#xff0c;一种是狼。 上帝对羊群说:「如果你们要狼&#xff0c;就给一只&#xff0c;任它随意咬你们。如果你们要狮子&#xff0c;就给…

搜索引擎蜘蛛的基本原理

搜索引擎用来爬行和访问页面的程序被称为蜘蛛&#xff0c;也叫做机器人。搜素引擎蜘蛛访问网站页面的时候就和你使用浏览器访问的过程一样&#xff0c;蜘蛛发出访问页面的请求&#xff0c;服务器会返回HTML代码&#xff0c;蜘蛛程序把这些代码存到原来页面的数据中&#xff0c;…

爱斯基摩人捕狼

爱斯基摩人捕猎狼的办法世代相传&#xff0c;很特别&#xff0c;也很有效。严冬季节&#xff0c;他们在锋利的刀刃上涂上一层新鲜的动物血。等血冻住后&#xff0c;他们再往上涂第二层血。再让血冻住&#xff0c;然后再涂……如此反复&#xff0c;很快刀刃就被冻血坨藏得严严实…

狼与羊

题目 青青草原上有100只狼和1只喜羊羊&#xff0c;狼可以吃草也可以吃羊&#xff0c;但是狼更喜欢吃羊。 ①如果狼吃了羊&#xff0c;就会变成羊&#xff0c;可能被其他狼吃掉。 ②假设这些狼都绝对聪明&#xff0c;他们的第一要务是生存。 ③一只羊只能被一只狼吃掉&#…

常见的搜索引擎蜘蛛有哪些

spider常见的SEO搜索引擎蜘蛛有 Baiduspider、Googlebot、bingbot、Bytespider、YisouSpider 等 常见蜘蛛整理 1.百度: baidu.com - Baiduspider 2.谷歌: google.com - Googlebot 3.必应蜘蛛&#xff1a;bing.com - bingbot 4.头条蜘蛛&#xff1a;toutiao.com - Bytespid…

海蜘蛛详细安装教程(图解)

海蜘蛛详细安装教程&#xff08;图解&#xff09; 打开VMware虚拟机&#xff0c;点击号添加新的虚拟机&#xff0c;选择自定义安装 添加一个网络适配器 加载iso文件 打开后一直enter即可 到此海蜘蛛安装成功&#xff0c;开始配置海蜘蛛路由