一道Java编程题目

bobo415 2009-12-22 10:36:01
编程输出下列矩阵
例如:
输入: 5
输出:
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
...全文
443 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
gigimyl520 2010-07-26
  • 打赏
  • 举报
回复
原來可以這樣 學習了
gao11811 2009-12-23
  • 打赏
  • 举报
回复
mark
sweetBug 2009-12-23
  • 打赏
  • 举报
回复
学习
yegaofei 2009-12-23
  • 打赏
  • 举报
回复
从中心点开始往回走,每走一部数值减一。 其中走的路线也是有规律的,横的走几格,必定伴随着纵向的也走几格。

ublic class FunClass {

/**
* @param args
*/
public static void main(String[] args) {
int arg = 5; //设置你要得数

int[][] arry = new int[arg][arg];
int centerColumn = 0;
int centerRow = 0;
if(arg % 2 == 0){
centerColumn = arg / 2 - 1;
centerRow = arg / 2 ;
} else {
centerColumn = arg / 2;
centerRow = arg / 2;
}

int value = arg * arg;
arry[centerRow][centerColumn] = value--;

boolean isMoveVertical = false;
boolean isToLeft = false;
boolean isUp = true;
if(arg % 2 == 0){
isToLeft = false;
isUp = true;
} else {
isToLeft = true;
isUp = false;
}
int currCol = centerColumn;
int currRow = centerRow;
for(int i = 1; i < arg; i++){
for(int j = 0 ; j < 2; j++){
for(int k = 0; k < i; k++){
if(isMoveVertical){
if(isUp){
currRow--;
} else {
currRow++;
}
} else {
if(isToLeft){
currCol--;
} else {
currCol++;
}
}
arry[currRow][currCol] = value--;
}
isMoveVertical = !isMoveVertical;
}
isToLeft = !isToLeft;
isUp = !isUp;
}

for(int i = 0; i < arg - 1; i++){
arry[0][i] = i+1;
}

//output the array
for(int i = 0; i < arg; i++){
for(int j = 0; j < arg; j++){
System.out.print(arry[i][j]);
System.out.print(" ");
}
System.out.println();
}

}

}
huosidun0302 2009-12-23
  • 打赏
  • 举报
回复
顶顶顶
xuhailang00 2009-12-23
  • 打赏
  • 举报
回复
很强大。
商科程序员 2009-12-23
  • 打赏
  • 举报
回复
这是我06年5月写的程序:
http://blog.csdn.net/chouy/archive/2006/06/15/800188.aspx
sweatcoffee 2009-12-23
  • 打赏
  • 举报
回复
学习
huanglinhao 2009-12-22
  • 打赏
  • 举报
回复
来顶一下 还有来学习学习
NetMatrix 2009-12-22
  • 打赏
  • 举报
回复
也过来学习一下
miguboy 2009-12-22
  • 打赏
  • 举报
回复
去wwww.chinajavaworld.com。你搜一下螺旋矩阵,里面有个很好的例子,讲解的和透彻,个人觉得很好。推荐。
nehnre 2009-12-22
  • 打赏
  • 举报
回复

public static void main(String args[]){
int constNum = 11;
int n=1;
//0 1 2 3 4 / 9 14 19 24 / 23 22 21 20 / 15 10 5 / 6 7 8 / 13 18 / 17 16 / 11 / 12
int [] test = new int[constNum*constNum];

int count = constNum;
boolean hasSub = true;
int []operator = new int[4];
operator[0] = 1;
operator[1] = constNum;
operator[2] = -1;
operator[3] = -constNum;
int index = 0;
int last = -1;
while (count > 0) {
for (int i = 0; i < count; i++) {
test[last + operator[index]] = n++;
last = last + operator[index];

}
index = (index +1)%4;
if(hasSub){
count--;
hasSub = false;
}
else{
hasSub = true;
}
}
for(int i=0;i<constNum*constNum;i++){
if(i%constNum==0){
System.out.println("");
}
System.out.print(test[i] + "\t");
}
}
nehnre 2009-12-22
  • 打赏
  • 举报
回复
public static void main(String args[]){
int constNum = 11;
int n=1;
//0 1 2 3 4 / 9 14 19 24 / 23 22 21 20 / 15 10 5 / 6 7 8 / 13 18 / 17 16 / 11 / 12
int [] test = new int[constNum*constNum];

int count = constNum;
boolean hasSub = true;
int []operator = new int[4];
operator[0] = 1;
operator[1] = constNum;
operator[2] = -1;
operator[3] = -constNum;
int index = 0;
int last = -1;
while (count > 0) {
for (int i = 0; i < count; i++) {
test[last + operator[index]] = n++;
last = last + operator[index];

}
index = (index +1)%4;
if(hasSub){
count--;
hasSub = false;
}
else{
hasSub = true;
}
}
for(int i=0;i<constNum*constNum;i++){
if(i%constNum==0){
System.out.println("");
}
System.out.print(test[i] + "\t");
}
}
zhuzeitou 2009-12-22
  • 打赏
  • 举报
回复

public class Matrix {
private int[][] matrix;
private int n;

public Matrix(int n) {
this.n = n;
matrix = new int[n][n];
int[] direct = new int[] { 1, 0, -1, 0 };
int directX = 0, directY = 3, currentX = -1, currentY = 0;
for (int count = 0; count < n * n;) {
int nextX = currentX + direct[directX], nextY = currentY
+ direct[directY];
if (nextX >= n || nextY >= n || nextX < 0 || nextY < 0
|| matrix[nextY][nextX] != 0) {
directX = (directX + 1) % 4;
directY = (directY + 1) % 4;
continue;
}
matrix[nextY][nextX] = ++count;
currentX = nextX;
currentY = nextY;
}
}

public String toString() {
StringBuffer sb = new StringBuffer();
if (matrix != null) {
int length = new String(n * n + "").length();
String format = "%-" + length + "d ";
for (int[] array : matrix) {
for (int i : array) {
sb.append(String.format(format, i));
}
sb.append('\n');
}
}
sb.trimToSize();
return sb.toString();
}
}
bobo415 2009-12-22
  • 打赏
  • 举报
回复
大家多顶下
希望能写个程序粘出来
欢迎不同见解
wanghuailong 2009-12-22
  • 打赏
  • 举报
回复
关注一下@
ComputerHeart 2009-12-22
  • 打赏
  • 举报
回复
链表?同意楼上的观点!分解成正方框。
zhuzeitou 2009-12-22
  • 打赏
  • 举报
回复
转向条件就是超过边界(小于0或者大于等于参数)或者即将到达的数组元素不为0
dlnu05610 2009-12-22
  • 打赏
  • 举报
回复

public static int[][] aa(int k){
int[][] hui = new int[k][k];
int row=0;
int fx=0;
for(int i=1;i<k*k;){
if(fx%4==0){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[row][j]=i;
i++;
}
fx++;
}else if(fx%4==1){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[j][k-row-1]=i;
i++;
}
fx++;
}else if(fx%4==2){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[k-1-row][k-j-1]=i;
i++;
}
fx++;
}else if(fx%4==3){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[k-j-1][row]=i;
i++;
}
fx++;
row++;
}
}

if(k%2==1){
hui[(int)(k/2)][(int)(k/2)]=k*k;
}else{
hui[k/2][k/2-1]=k*k;
}

return hui;
}
zhuzeitou 2009-12-22
  • 打赏
  • 举报
回复
个人方法:
int[] direct = {1, 0, -1, 0};

初始定义
int directX = 0, directY = 3;

需要转向时
directX = (directX + 1) % 4;
directY = (directY + 1) % 4;
加载更多回复(10)

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧