| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 714 人关注过本帖
标题:矩阵输出
只看楼主 加入收藏
yanmin0614
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-7-22
收藏
 问题点数:0 回复次数:7 
矩阵输出

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│
└─┴─┴─┴─┴─┘ └─┴─┴─┴─┴─┘ └─┴─┴─┴─┴─┘

谁能编出来,谢谢了

搜索更多相关主题的帖子: 输出 矩阵 
2007-10-03 09:48
认识铁哥不
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-10-1
收藏
得分:0 

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

2007-10-03 12:51
睫毛上的土
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-8-13
收藏
得分:0 
回复:(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();
}
}

爸爸给买了《Core Java》,厚厚的两大本诶,啥时候能够看完呢~
2007-10-04 05:53
睫毛上的土
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-8-13
收藏
得分:0 
其他两个都好弄的,中间那个弄是弄出来了,代码臭是真的臭的可以,马马虎虎将就着看吧。

困了,睡觉去了~

爸爸给买了《Core Java》,厚厚的两大本诶,啥时候能够看完呢~
2007-10-04 05:53
yanmin0614
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-7-22
收藏
得分:0 
怎么修改我的头像啊,谢谢

2007-10-04 09:14
justing_xxt
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2005-3-9
收藏
得分:0 

[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]
2007-10-05 15:35
yanmin0614
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-7-22
收藏
得分:0 
谢谢楼上兄弟

2007-10-06 16:12
batwyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:88
专家分:0
注 册:2007-4-16
收藏
得分:0 
回复:(justing_xxt)[em25][em25][em25][em25][CODE...
顶!!!!算法不错,主要是代码写得很规范,大家都看下!

2007-10-06 18:50
快速回复:矩阵输出
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022645 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved