/**
* @author qzw
*/
public class MultiMatrix {
public static void main(String args[]){
//逆时针输出回形矩阵图
int[][] arr = getArray(4,4);
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+",");
}
System.out.println();
}
}
/**
* 获取xInt * yInt逆时针回形矩阵数组
* @param xInt,矩阵x轴大小
* @param yInt,矩阵y轴大小
* @return
*/
public static int[][] getArray(int xInt, int yInt){
int xMax = xInt;
int yMax = yInt;
int n = 4;
int arr[][]=new int[yMax][xMax];
int x = 0, y = 0;
int xMin = 0, yMin = 0;
int size = xMax * yMax;
boolean flag = true;
for(int i=0;i<size;i++){
arr[y][x] = i+1;
if((y+1)<yMax && flag){
y++;
}else if((x+1)<xMax && flag){
x++;
}else {
if(y>yMin){
y--;
}else if(x>(xMin+1)){
x--;
}else{
xMax--;
yMax--;
xMin++;
yMin++;
y++;
flag = true;
}
}
if((y+1) == yMax && (x+1) == xMax){
flag = false;
}
}
return arr;
}
}
解决方案 »
- 有关package中引用类的问题???大家帮帮忙,多谢啦!!!
- 求!用java写的石头剪子布游戏改进版代码,要求两台机子联机,用到客户端与服务端
- 运行是看不见列表内容
- java基础问题,谢谢!!
- 请问:使用POI遍历一个EXCEL文件的时候出现的问题
- 求救!!!!!zip包里有中文文件名解压时报错
- ★☆java收我用vb+winsock写的数据有问题~收到null,或是关闭winsock后才能收到★☆
- 高手请进,applet 与 servlet 通讯问题
- 求救各位大神关于做JFrame不规则半透明窗口
- 请教Java Swing 搜狗输入法的问题
- new double[3 + generator.nextInt(4)];是什么意思?新手求助
- 一个简单的关于Long判断问题
arr[y][x] = i+1;//更改为 arr[x][y] = i+1;就能达到你的要求了
* @author qzw
*/
public class MultiMatrix {
public static void main(String args[]){
//逆时针输出回形矩阵图
int[][] arr = getArray(4,4);
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
//System.out.print(arr[i][j]+",");
System.out.printf("%03d",arr[i][j]);
System.out.print("\t");
}
System.out.println();
}
}
/**
* 获取xInt * yInt逆时针回形矩阵数组
* @param xInt,矩阵x轴大小
* @param yInt,矩阵y轴大小
* @return
*/
public static int[][] getArray(int xInt, int yInt){
int xMax = xInt;
int yMax = yInt;
int n = 4;
int arr[][]=new int[yMax][xMax];
int x = 0, y = 0;
int xMin = 0, yMin = 0;
int size = xMax * yMax;
boolean flag = true;
for(int i=0;i<size;i++){
arr[x][y] = i+1;
if((y+1)<yMax && flag){
y++;
}else if((x+1)<xMax && flag){
x++;
}else {
if(y>yMin){
y--;
}else if(x>(xMin+1)){
x--;
}else{
xMax--;
yMax--;
xMin++;
yMin++;
y++;
flag = true;
}
}
if((y+1) == yMax && (x+1) == xMax){
flag = false;
}
}
return arr;
}
}
1、输入n后,创建一个n*n的二维数组,将1到n*n这些数字填如数组。
2、当然1填入的数组的下标为(0,0)接着下一个数字呐?所以要定义一个参数方向
具体代码:import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;public class Test {
public static void main(String[] args) throws Exception {
String line="";
while(line!=null&&!"q".equalsIgnoreCase(line)){
System.out.println("请输入大于零的数字(输入q退出)");
Scanner sc=new Scanner(System.in);
line=sc.nextLine();
if(line!=null&&line.matches("\\d++")){
int n=Integer.parseInt(line);
int[][] ints=FillNumber(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(ints[i][j]+"\t");
}
System.out.println();
}
}
}
} private static int[][] FillNumber(int n) {
int[][] ints=new int[n][n];
int x=0;
int y=0;
int direction=0;
Set<String> used=new HashSet<String>();
for(int num=1;num<=n*n;num++){
// System.out.println("("+x+","+y+")="+num);
ints[x][y]=num;
direction=nextDirection(x,y,direction,n,used);
x=nextX(x,direction,n);
y=nextY(y,direction,n);
}
return ints;
}
private static int nextY(int y, int direction,int n) {
if(direction==0){
return y+1;
}else if(direction==2){
return y-1;
}else{
return y;
}
} private static int nextX(int x, int direction,int n) {
if(direction==1){
return x+1;
}else if(direction==3){
return x-1;
}else{
return x;
}
} /**
* 确定下一个数的方向,0 y+1;1 x+1;2 y-1; 3 x-1
* @param x
* @param y
* @param direction 下一个数填充的方向
* @param n 边界
* @return
*/
private static int nextDirection(int x, int y, int direction, int n,Set<String> used) {
used.add(x+","+y);
boolean changeDirection=false;
int x2=nextX(x,direction,n);
if(x2<0||x2>=n){
changeDirection=true;
}
int y2=nextY(y,direction,n);
if(y2<0||y2>=n){
changeDirection=true;
}
String xy2=x2+","+y2;
if(used.contains(xy2)){
changeDirection=true;
}
if(changeDirection){
return (direction+1)%4;
}else{
return direction;
}
}}