逆序输出回形数组
来源:互联网 发布:java中的接口和抽象类 编辑:程序博客网 时间:2024/05/22 06:13
课上老师随意提了一下,有位师兄与一份不错的工作失之交臂,就是因为没有写出回形数组的题目,好吧,我立马上心了。而且觉得题目蛮有趣的,初学数组的我对此很感兴趣。刚开始的思路是想给二维数组的最外环赋值,然后再一层一层递进,后来发觉这样很繁琐。后来在草稿纸上画了几次,突然茅塞顿开,找到了数组下标的规律,很快就做出来了,蛮开心的~
package norah.javase.exer;
//2015-09-19
import java.util.Scanner;
public classZigZag3 {
publicstatic void main(String[]args) {
System.out.println("请输入回形数组的行列数:");
Scannerscanner=new Scanner(System.in);
intn = scanner.nextInt();
ZigZag(n);
}
publicstatic void ZigZag(intn) {
int[][]zigzag1 = new int[n][n];
intnum = 1;
intlineplus = 0;
introwplus = 0;// 记录递增的列数
intlinedecrease =n - 1; // 记录递减的行数
introwdecrease =n - 1; // 记录递减的列数
for (intk = 0; k < Math.round(n/2);k++) {
for (intj = rowplus; j < rowdecrease;j++) {
zigzag1[lineplus][j] = num++;
}
for (inti = lineplus; i < linedecrease;i++) {
zigzag1[i][rowdecrease] = num++;
}
for (intj = rowdecrease;j > rowplus;j--) {
zigzag1[linedecrease][j] =num++;
}
for (inti = linedecrease;i > lineplus;i--) {
zigzag1[i][lineplus] = num++;
}
lineplus++;
rowplus++;
linedecrease--;
rowdecrease--;
if(lineplus ==rowdecrease){
zigzag1[lineplus][rowdecrease] = n*n;
}
}
for (inti = 0; i < n; i++) {
for (intj = 0; j < n; j++) {
System.out.print(zigzag1[i][j] + "\t");
}
System.out.println();
}
}
}
- 逆序输出回形数组
- 输出回形数组
- 回形数组的输出
- 回形数组
- 小程序(1)——输出S形数组
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- 逆序输出
- java 回形数存储
- Linux中的程序和进程,PID和PPID
- ios9兼容系列之解决iPad提交报错“iPad Multitasking support requires launch story board in bundle '”
- 一张图说明操作系统的主要结构功能
- 关于开启XAMPP的Apache服务报错端口被占用的解决方法
- java基本类型与包装类型
- 逆序输出回形数组
- Navicat for Oracle 表名不存在 ORA00903
- Mantis的使用
- [note]构建常量
- 二叉树的宽度和深度
- python serial 模块
- 第四周-项目2-建设单链表算法库
- 剑指offer--二叉搜索树的后序遍历序列
- Xml遍历读取