java逆时针螺旋输出矩阵
来源:互联网 发布:剩余windows重置计数2 编辑:程序博客网 时间:2024/06/05 16:44
import java.util.Scanner;
/**
*@author Sun
* @since
* @version 1.0
*
**/
public class test1 {
public static void main(String args[]) {
//获取传入的值
Scanner sc=new Scanner(System.in);
int nextInt = sc.nextInt();
int[][] arr = gettx(nextInt, nextInt);
//逆时针循环输出值
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
//如果小于10就在前面加0
if (arr[i][j] < 10) {
System.out.print("0"+arr[i][j] + "\t");
} else {
System.out.print(arr[i][j] + "\t");
}
}
System.out.println();
}
}
/**
* 换行 获取值jInt jyInt
*@author Sun
* @retuen 无返回
*/
public static int[][] gettx(int jInt, int jyInt) {
int jMax = jInt;
int yyMax = jyInt;
int arr[][] = new int[yyMax][jMax];
int x = 0, y = 0;
int xMin = 0, yMin = 0;
int size = jMax * yyMax;
boolean flag = true;
for (int i = 0; i < size; i++) {
arr[y][x] = i + 1;
if ((y + 1) < yyMax && flag) {
y++;
} else if ((x + 1) < jMax && flag) {
x++;
} else {
if (y > yMin) {
y--;
} else if (x > (xMin + 1)) {
x--;
} else {
jMax--;
yyMax--;
xMin++;
yMin++;
y++;
flag = true;
}
}
if ((y + 1) == yyMax && (x + 1) == jMax) {
flag = false;
}
}
return arr;
}
}
/**
*@author Sun
* @since
* @version 1.0
*
**/
public class test1 {
public static void main(String args[]) {
//获取传入的值
Scanner sc=new Scanner(System.in);
int nextInt = sc.nextInt();
int[][] arr = gettx(nextInt, nextInt);
//逆时针循环输出值
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
//如果小于10就在前面加0
if (arr[i][j] < 10) {
System.out.print("0"+arr[i][j] + "\t");
} else {
System.out.print(arr[i][j] + "\t");
}
}
System.out.println();
}
}
/**
* 换行 获取值jInt jyInt
*@author Sun
* @retuen 无返回
*/
public static int[][] gettx(int jInt, int jyInt) {
int jMax = jInt;
int yyMax = jyInt;
int arr[][] = new int[yyMax][jMax];
int x = 0, y = 0;
int xMin = 0, yMin = 0;
int size = jMax * yyMax;
boolean flag = true;
for (int i = 0; i < size; i++) {
arr[y][x] = i + 1;
if ((y + 1) < yyMax && flag) {
y++;
} else if ((x + 1) < jMax && flag) {
x++;
} else {
if (y > yMin) {
y--;
} else if (x > (xMin + 1)) {
x--;
} else {
jMax--;
yyMax--;
xMin++;
yMin++;
y++;
flag = true;
}
}
if ((y + 1) == yyMax && (x + 1) == jMax) {
flag = false;
}
}
return arr;
}
}
0 0
- java逆时针螺旋输出矩阵
- Java输出逆时针螺旋数字
- java蛇形矩阵输出方法(顺时针、逆时针)
- 逆时针顺序的螺旋矩阵打印
- 螺旋矩阵 有内及外 逆时针
- 逆时针输出回形矩阵图(Java版本)
- java 输出回环数(螺旋矩阵)
- 输出螺旋矩阵(一)
- 输出螺旋矩阵(二)
- 输出螺旋矩阵(三)
- 矩阵式螺旋输出
- 矩阵式螺旋输出
- 矩阵式螺旋输出
- 矩阵式螺旋输出
- 输出螺旋矩阵
- 输出螺旋矩阵
- 输出螺旋矩阵
- 输出螺旋矩阵
- 关于NIO的理解及相关的例子
- 贝塞尔曲线拟合原理
- Android 压缩图片并转Base64方法之一
- google protobuf src安装运行
- 10年软件测试工作总结
- java逆时针螺旋输出矩阵
- JS数组的去重与降维
- 批量创建neo4j中的节点的时候,如果需要用到现有的节点需要使用with连接起来
- Ubuntu Android OpenJDK 1.6、1.7、1.8、1.9下载安装
- 深入android源码---apk生成过程综述
- 解决IOS滑动触发Body的滑动
- android客户端首次进入轮播引导图(亖)
- Gridview 关于添加静态数据的快速方法
- Java SE学习笔记:File类、IO流以及他们的经典案例