编程输出下列矩阵
例如:
输入: 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
例如:
输入: 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
解决方案 »
- 迅雷笔试的一道多线程题目解答
- 问一个小问题,谢谢!
- 如何从网管升级到程序员
- 用java怎么实现定时关机?关机是不是调用某个api函数,这个api是什么? 先谢了
- 从给定文件中读取一个整数,并将其进行排序,然后再写入另一个文件中,谢谢,帮助编写一个程序!!
- 只能用awt来写界面,怎么办啊?感觉写出来不美观阿?
- 寻合作者:共同阅读国外收费在线图书(英文原著作,1400余部计算机专业图书,涵盖O'Reilly出版社的全部力作)
- MaskFormatter的 setMask函数编译不了 ?
- Boolean”是什么意思????
- 这是什么原因呀
- 各位大虾,有没有用过用JAVA来实现lzx压缩的?
- 如何使JTree中个节点被选中!
自己也有方法了 就行不怎么行
什么用for循环什么的 就不要说了
懂编程的都会知道
问题是怎么控制
int[] direct = {1, 0, -1, 0};初始定义
int directX = 0, directY = 3;需要转向时
directX = (directX + 1) % 4;
directY = (directY + 1) % 4;
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();
}
}
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");
}
}
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");
}
}
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;
}
http://blog.csdn.net/chouy/archive/2006/06/15/800188.aspx
* @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();
} }}