当前位置: 代码迷 >> Java相关 >> 矩阵输出
  详细解决方案

矩阵输出

热度:667   发布时间:2007-10-03 09:48:17.0
矩阵输出

6. 矩阵中填数. 当给出 N*N 的矩阵,要求用程序填入下列形式的数:

① 倒填,例如N=5 ② 蛇形填数 ③ 回转填数

┌─┬─┬─┬─┬─┐ ┌─┬─┬─┬─┬─┐ ┌─┬─┬─┬─┬─┐
│25│24│23│22│21│ │ 1│ 3│ 4│10│11│ │ 1│16│15│14│13│
├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤
│20│19│18│17│16│ │ 2│ 5│ 9│12│19│ │ 2│17│24│23│12│
├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤
│15│14│13│12│11│ │ 6│ 8│13│18│20│ │ 3│18│25│22│11│
├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤
│10│ 9│ 8│ 7│ 6│ │ 7│14│17│21│24│ │ 4│19│20│21│10│
├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤ ├─┼─┼─┼─┼─┤
│ 5│ 4│ 3│ 2│ 1│ │15│16│22│23│25│ │ 5│ 6│ 7│ 8│ 9│
└─┴─┴─┴─┴─┘ └─┴─┴─┴─┴─┘ └─┴─┴─┴─┴─┘

谁能编出来,谢谢了

搜索更多相关的解决方案: 输出  矩阵  

----------------解决方案--------------------------------------------------------

比较麻烦,两个for循环搞定


----------------解决方案--------------------------------------------------------
回复:(yanmin0614)矩阵输出
public class Matrix {
private int[][] matrix;
public Matrix(int cLength){
this.matrix = new int[cLength][cLength];

int x = 0, y = 0;
int stepX = -1;
int stepY = 1;
for(int cnt = 1; cnt < 26; cnt ++){
matrix[x][y] = cnt;
if((x == 0 && stepX < 0)||(x == cLength -1 && stepX > 0)){
if(y < cLength - 1)
y ++;
else
x ++;
stepX *= -1;
stepY *= -1;
}
else if((y == 0 && stepY < 0)||(y == cLength -1 && stepY > 0)){
if(x < cLength -1)
x ++;
else
y ++;
stepX *= -1;
stepY *= -1;
}
else {
x += stepX;
y += stepY;
}
}
}

public void drawMatrix(){
for(int x = 0; x < 5; x ++){
for(int y = 0; y < 5; y++){
System.out.printf("[%2d]", matrix[x][y]);
}
System.out.printf("\n");
}
}

public static void main(String[] args){
Matrix m = new Matrix(5);
m.drawMatrix();
}
}

----------------解决方案--------------------------------------------------------
其他两个都好弄的,中间那个弄是弄出来了,代码臭是真的臭的可以,马马虎虎将就着看吧。

困了,睡觉去了~

----------------解决方案--------------------------------------------------------
怎么修改我的头像啊,谢谢
----------------解决方案--------------------------------------------------------

[CODE]

public class Matrix {
private int size = 0;

private int[][] converseMatrix;
private int[][] snakeMatrix;
private int[][] roundMatrix;

public Matrix(int size) {
this.size = size;
converseMatrix = new int[size][size];
snakeMatrix = new int[size][size];
roundMatrix = new int[size][size];
}

// 构造倒置矩阵
public void fillConverseMatrix() {
int total = size * size;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
converseMatrix[i][j] = total--;
}
}
}

// 构造蛇形矩阵
public void fillSnakeMatrix() {
int increase = 1, decrease = size*size;
for (int i = 0; i < size; i++) {
int m = 0, n = i;
for (int j = 0; j <= i; j++) {
if (i%2 == 0) {
snakeMatrix[m++][n--] = increase++;
snakeMatrix[size-1-m+1][size-1-n-1] = decrease--;
} else {
snakeMatrix[n--][m++] = increase++;
snakeMatrix[size-1-n-1][size-1-m+1] = decrease--;
}
}
}
}

// 构造回转矩阵
public void fillRoundMatrix() {
int roundLength = size, total = 1;
for (int i = 0; i < (size+1)/2; i++) {
for (int j=0; j < roundLength-1; j++) {
roundMatrix[j+i][i] = total++;
roundMatrix[size-1-i][j+i] = (total - 1) + (roundLength - 1);
roundMatrix[size-1-j-i][size-1-i] = (total - 1) + (roundLength - 1) * 2;
roundMatrix[i][size-1-j-i] = (total - 1) + (roundLength - 1) * 3;
}
roundLength -= 2;
total = roundMatrix[i][i+1] + 1;
}
if (size%2 == 1) {
roundMatrix[size/2][size/2] = size*size;
}
}

// 打印矩阵
public void print(int[][] matrix) {
int size = matrix.length;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.printf("[%2d]", matrix[i][j]);
}
System.out.println();
}
}

public static void main(String[] args) {
Matrix test = new Matrix(5);
test.fillConverseMatrix();
test.fillSnakeMatrix();
test.fillRoundMatrix();

System.out.println("倒置矩阵:");
test.print(test.converseMatrix);
System.out.println("蛇形矩阵:");
test.print(test.snakeMatrix);
System.out.println("回转矩阵:");
test.print(test.roundMatrix);
}
}

[/CODE]
----------------解决方案--------------------------------------------------------
谢谢楼上兄弟
----------------解决方案--------------------------------------------------------
回复:(justing_xxt)[em25][em25][em25][em25][CODE...
顶!!!!算法不错,主要是代码写得很规范,大家都看下!
----------------解决方案--------------------------------------------------------
  相关解决方案