网上找的import java.util.*;public class test { //Main method public static void main(String[] args) { int row = Integer.parseInt(args[0]); int col = Integer.parseInt(args[1]);
int count = row*col; int mintemp = 1; int rowtemp = row; int coltemp = col;
Vector allvec = new Vector(); while (mintemp <= count) { Vector vec = getvecs(mintemp, rowtemp, coltemp); allvec.add(vec);
帮助大一些实在有问题请发邮件到[email protected],我有源码
{
//Main method
public static void main(String[] args)
{
int row = Integer.parseInt(args[0]);
int col = Integer.parseInt(args[1]);
int count = row*col;
int mintemp = 1;
int rowtemp = row;
int coltemp = col;
Vector allvec = new Vector();
while (mintemp <= count)
{
Vector vec = getvecs(mintemp, rowtemp, coltemp);
allvec.add(vec);
mintemp = mintemp + coltemp - 1 + rowtemp - 1 + coltemp - 1 + rowtemp - 1;
rowtemp = rowtemp - 2;
coltemp = coltemp - 2;
}
String[][] all = new String[row+1][col+1];
int minrow = 1;
int maxrow = row;
int mincol = 1;
int maxcol = col;
for (int i=0;i<allvec.size();i++)
{
Vector vec = (Vector)allvec.elementAt(i);
Vector row1 = (Vector)vec.elementAt(0);
Vector row2 = (Vector)vec.elementAt(1);
Vector col1 = (Vector)vec.elementAt(2);
Vector col2 = (Vector)vec.elementAt(3);
for (int j=0, tt = 0;j<row1.size();j++, tt++)
all[minrow][mincol+tt] = (String)row1.elementAt(j);
for (int j=row2.size()-1, tt = 0;j>=0;j--, tt++)
all[maxrow][mincol+tt] = (String)row2.elementAt(j);
for (int j = 0, tt = 1; j < col1.size(); j++, tt++)
all[minrow+tt][maxcol] = (String)col1.elementAt(j);
for (int j=col2.size()-1, tt = 1;j>=0;j--, tt++)
all[minrow+tt][mincol] = (String)col2.elementAt(j);
minrow = minrow + 1;
mincol = mincol + 1;
maxrow = maxrow - 1;
maxcol = maxcol - 1;
}
for (int i=1;i<=row;i++)
{
for (int j=1;j<=col;j++)
{
System.out.print(dd(all[i][j])+" ");
}
System.out.println(" ");
}
}
static private String dd(String ss)
{
String pp =ss;
if (ss.length()==1)
pp=" "+ss;
else if (ss.length()==2)
pp=" "+ss;
return pp;
}
static private Vector getvecs(int min,int row,int col)
{
Vector allvec = new Vector();
Vector row1 = new Vector();
Vector row2 = new Vector();
Vector col1 = new Vector();
Vector col2 = new Vector();
allvec.add(row1);
allvec.add(row2);
allvec.add(col1);
allvec.add(col2);
if(row == 1)
{
for (int i = 1; i <= col; i++)
row1.add(String.valueOf(min + i - 1));
}
else if(col == 1)
{
row1.add(String.valueOf(min));
for (int i = 1; i < row; i++)
col1.add(String.valueOf(min + i));
}
else
{
for (int i = 1; i <= col; i++)
{
row1.add(String.valueOf(min + i - 1));
row2.add(String.valueOf(min + col - 1 + row - 1 + i - 1));
}
for (int i = 1; i < row-1; i++)
{
col1.add(String.valueOf(min + col + i - 1));
col2.add(String.valueOf(min + col - 1 + row - 1 + col + i - 1));
}
}
return allvec;
}
}
* 基本思路:采用数学方法直接计算出矩阵元素P(x,y)的值.
* 将整个矩阵看成由外道内的矩形圈组成,矩阵中任意一点P(x,y)的值=
* 位于外圈的所有点数+位于本圈上所有点数+1
*/
public class Martrix
{
private int H;
private int L; public static void main(String argv[])
{
Martrix m1=new Martrix(6,6);
m1.print();
}
public Martrix()
{
this.H=3;
this.L=3;
}
public Martrix(int x,int y)
{
this.H=x;
this.L=y;
} //计算处于点p(x,y)前面的所有点数
public int getDotCount(int x,int y)
{
return getDotCount1(H,L,x,y) + getDotCount2(H,L,x,y) + 1;
} //计算点P(x,y)外围矩形圈数
public static int getN(int H,int L,int x,int y)
{
return Math.min(Math.min(x,y),Math.min((H-x),(L-y)));
} //计算点P(x,y)外围矩形圈上的点数 (等差数列 )
public static int getDotCount1(int H,int L,int x,int y)
{
int N=getN(H,L,x,y);
int S1=2*(H+L);
int S2=2*(H+L)-8*N + 8;
return (S1+S2)/2 * N;
} //计算与点P(x,y)处于同一个矩形圈上,且在点P前面的所有点数 (分段函数)
public static int getDotCount2(int H,int L,int x,int y)
{
int N=getN(H,L,x,y);
int x1=x-N;
int y1=y-N;
int H1=H-2*N;
int L1=L-2*N;
int count;
if(L1==0) //特列:H >=L ,L为奇数,y=(L-1)/2 实例(H=6,L=5,x=3,y=3)
return x1 ; if(H1==0) //特列: H < L ,H为奇数,x=(H-1)/2 实例(H=5,L=6,x=3,y=3)
return y1;
if(y1==0) //一般情况:
{
count=x1;
}
else if(x1==H1) //一般情况:
{
count=H1 + y1;
}
else if(y1==L1) //一般情况:
{
count=H1 + L1 + (H1 - x1);
}
else if(x1==0) //一般情况:
{
count=H1 + L1 + (H1 - x1) + (L1 - y1);
}
else //出错情况:
{
count= -1;
} return count;
} //计算并输出螺旋矩阵
public void print()
{
System.out.println("H = " + H +" , " + "L = " + L);
System.out.println("******************************************");
for(int j=0;j<=L;j++)
{
for(int i=0;i<=H;i++)
{
System.out.print(getDotCount(i,j)+" ");
}
System.out.println();
}
System.out.println("************************************");
}
}
所以网上找的程序我都没有调试过,不知道是否正确import java.io.*;
class ScrewMatrix
{
public static void main(String[] args)
{
int width=10; int height=9; //初始化矩阵的行和列
int r=width*height+1;
int w=width; int h=height-1;
int n=0; int g=0;
int p=h-1; int m=width;
int count=1; int f=0;
int []arr=new int[r-1];
while(true)
{
for(int i=0;(i<w)&&(count!=r);++i,++count)
arr[width*n+i+(width-m)-f]=count;
for(int i=0;(i<h)&&(count!=r);++i,++count)
arr[width*(height-p+i)-g-1]=count; for(int i=0;(i<(w-1))&&(count!=r);++i,++count)
arr[width*(h+g+1)-g-1-i-1]=count; for(int i=0;(i<(h-1))&&(count!=r);++i,++count)
arr[width*(p-i)+g+1-1]=count;if(count==r)
break; --p; m-=2; ++n; ++g; h-=2; w-=2; ++f;
}for(int i=0;i<arr.length;++i)
{
if(i%(width)==0)
System.out.println();
System.out.print(" "+arr[i]+'\t');
}
System.out.println();
}
}
public class LXArray{
private int[][] result;
private int size;
public LXArray(int size){
this.size = size;
result = new int[size][size];
}
public void compute(){
compute(1,0,0,size - 1,size - 1);
//result矩阵为所求
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++)
System.out.print(result[i][j] + " ");
System.out.println();
}
}
private void compute(int k,int l,int t,int r,int b){
if(l == size/2) {
if(size % 2 == 1)
result[t][r] = k;
return;
}
int i = t,
j = r;
while(i < b)
result[i++][j] = k++;
while(j > l)
result[i][j--] = k++;
while(i > t)
result[i--][j] = k++;
while(j < r)
result[i][j++] = k++;
compute(k,l+1,t+1,r-1,b-1);
}
public static void main(String[] args){
LXArray lxa = new LXArray(5);//使用
lxa.compute();
}
}