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如何实现啊???? 希望大家帮忙,谢谢了!!!!!
这样写那里不对?????
import java.util.Scanner;
public class Test55fangzhen {
public static void main(String[] args) {
int[][] a=new int[5][5];
int i,j,k=0,m,n;
Scanner in=new Scanner(System.in );
System.out.println("name");
n=in.nextInt();
if(n%2==0)m=n/2;
else m=n/2+1 ;
for(i=0;i<m;i++){
for(j=i;j<n;j++){k++;a[i][j]=k;}
for(j=i+1;j<n-i;j++){k++;a[i][j]=k;}
for(j=n-i-2;j>=i;j--){k++;a[n-i-1][j]=k;}
for(j=n-i-2;j>=i;j--){k++;a[j][i]=k;}
}
for(i=0;i<a.length;i++)
{ for(j=0;j<a[1].length;j++)
System.out.print("\t"+a[i][j]);
System.out .println();
}
}
}
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
用java如何实现啊???? 希望大家帮忙,谢谢了!!!!!
这样写那里不对?????
import java.util.Scanner;
public class Test55fangzhen {
public static void main(String[] args) {
int[][] a=new int[5][5];
int i,j,k=0,m,n;
Scanner in=new Scanner(System.in );
System.out.println("name");
n=in.nextInt();
if(n%2==0)m=n/2;
else m=n/2+1 ;
for(i=0;i<m;i++){
for(j=i;j<n;j++){k++;a[i][j]=k;}
for(j=i+1;j<n-i;j++){k++;a[i][j]=k;}
for(j=n-i-2;j>=i;j--){k++;a[n-i-1][j]=k;}
for(j=n-i-2;j>=i;j--){k++;a[j][i]=k;}
}
for(i=0;i<a.length;i++)
{ for(j=0;j<a[1].length;j++)
System.out.print("\t"+a[i][j]);
System.out .println();
}
}
}
解决方案 »
- netbeans 开发RCP时,新文件支持中怎么运用“新建文件”的问题
- 从1~31个数字当中随机抽出8位数字等于一个指定的数字
- java 文件的读取操作中选择一个特定的值的属性是?
- 求教:Collections.reverse()为什么不能将数组元素的顺序颠倒?
- 求“青岛本地”JAVA高手,需做SWING,GUI做客户端界面,对数据库进行调用,配合MYSQL,有偿!!具体事宜详谈!
- linux yum error,这个error已经困扰我好一段时间了,各位大哥哥指点一下小弟,感激不尽
- 线程经验丰富的人请进
- [算法求解]一道困惑爱因斯坦N年的数学题
- 关于scjp的一道题.郁闷求解.
- 截取字符串
- 菜鸟求问:这个程序怎么回事?
- 关于线程的一段代码错误
呵呵,好象很多人问这类问题.还有for循环尽量少套几层,
想别的办法代替,
要不容易出现溢出问题.
:)
public final int right=1;
public final int down=2;
public final int left=3;
public final int up=4;
public void printSnake(int n,int type) {
int legs=(int) Math.sqrt(n);
int[][] snake=new int[legs][legs];
for(int j=0; j<legs; j++)
for(int k=0; k<legs; k++)
snake[j][k]=0;
int i=0;
int direct=0;
int x=0;
int y=0;
if(type==1)
direct=right;
else
direct=down;
while(i<n) {
i++;
snake[y][x]=i;
if(type==1) {
if((direct==right && x==legs-1) ||(direct==right && snake[y][x+1]>0))
direct=down;
if((direct==down && y==legs-1) || (direct==down && snake[y+1][x]>0))
direct=left;
if((direct==left && x==0) || (direct==left && snake[y][x-1]>0))
direct=up;
if((direct==up && y==0) || (direct==up && snake[y-1][x]>0))
direct=right;
} else {
if((direct==right && x==legs-1) ||(direct==right && snake[y][x+1]>0))
direct=up;
if((direct==down && y==legs-1) || (direct==down && snake[y+1][x]>0))
direct=right;
if((direct==left && x==0) || (direct==left && snake[y][x-1]>0))
direct=down;
if((direct==up && y==0) || (direct==up && snake[y-1][x]>0))
direct=left;
}
if(direct==right)
x++;
if(direct==down)
y++;
if(direct==left)
x--;
if(direct==up)
y--;
}
for(int j=0; j<legs; j++) {
for(int k=0; k<legs; k++)
System.out.print(snake[j][k]+"\t");
System.out.println();
}
}
public static void main(String[] args) {
new Turner().printSnake(5*5,2);
}
}
以前写的方法,类似于贪吃蛇行走路线,有2个参数,第二个参数为1时是顺时针,其他时是逆时针。
private int direction; private int[] array; private int side;// 正方形边的大小 public FiveSquare(int side) {
array = new int[side * side];
this.side = side;
direction = 0;// 向右
} public void fill() {
int site = 0;
int next = 0;
for (int i = 1; i <= side * side; i++) {
boolean goNext = true;
array[site] = i;
if (i == side * side)
break;
switch (direction) {
// 向右这种情况
case 0:
next = site + 1;
if (next % side == 0 || array[next] != 0)
goNext = false;
break;
// 向下这种情况
case 1:
next = site + side;
if (next >= side * side || array[next] != 0)
goNext = false;
break;
// 向左
case 2:
next = site - 1;
if ((next + 1) % side == 0 || array[next] != 0)
goNext = false;
break;
// 向上
case 3:
next = site - side;
if (next <= 0 || array[next] != 0)
goNext = false;
break;
default:
break;
}
if (goNext) {
site = next;
} else {
direction = (direction + 1) % 4;
i--;
}
}
} public void printOut() {
for (int i = 0; i < side; i++) {
for (int j = 0; j < side; j++) {
int current = array[i * side + j];
String cur = Integer.toString(current);
if (cur.length() == 1)
cur = " " + cur;
System.out.print(cur + " ");
}
System.out.println();
}
} public static void main(String[] args) {
FiveSquare square = new FiveSquare(5);
square.fill();
square.printOut(); }}
int[][] xy = new int[5][5];
int x = 0, y = 0;
int tx = 1, ty = 0;
int maxx = 4, minx = 0;
int maxy = 4, miny = 1;
for (int n = 1; n <= 25; n++) {
xy[y][x] = n;
if (tx > 0 && x >= maxx) {
x = maxx;
ty = 1;
tx = 0;
maxx--;
}
else if (tx < 0 && x <= minx) {
x = minx;
tx = 0;
ty = -1;
minx++;
}
else if (ty > 0 && y >= maxy) {
y = maxy;
tx = -1;
ty = 0;
maxy--;
}
else if (ty < 0 && y <= miny) {
y = miny;
tx = 1;
ty = 0;
miny++;
}
x += tx;
y += ty;
} for (int i = 0; i < 5; i ++) {
for (int j = 0; j < 5; j++) {
System.out.printf("%02d ",xy[i][j]);
}
System.out.println();
}
}
public class Test{
public static void main(String[] args){
int[][] data=new int[5][5];
int i=0,j=0,ccnt=0,iway=1,jway=0;
while(ccnt++<=data.length*data[0].length){
data[i][j]=ccnt;
if(i+iway<0||i+iway>4||j+jway<0||j+jway>4||data[i+iway][j+jway]!=0){
if(iway==0){
iway=-1*jway;
jway=0;
}else{
jway=iway;
iway=0;
}
}
i+=iway;
j+=jway;
}
for(int n=0;n<5;n++){
for(int m=0;m<5;m++){
if(data[m][n]<10) System.out.print(" ");
System.out.print(data[m][n]+" ");
}
System.out.println();
}
}
}
public class Test{
public static void main(String[] args){
int[][] data=new int[5][5];
int i=0,j=0,ccnt=0,iway=1,jway=0;
while(ccnt++<=data.length*data[0].length){
data[i][j]=ccnt;
if(i+iway<0||i+iway>=data.length||j+jway<0||j+jway>=data[0].length||data[i+iway][j+jway]!=0){
if(iway==0){
iway=-1*jway;
jway=0;
}else{
jway=iway;
iway=0;
}
}
i+=iway;
j+=jway;
}
for(int n=0;n<data[0].length;n++){
for(int m=0;m<data.length;m++){
if(data[m][n]<10) System.out.print(" ");
System.out.print(data[m][n]+" ");
}
System.out.println();
}
}
}
System.out.println("16 17 18 19 6\n");
System.out.println("15 24 25 20 7\n");
System.out.println("14 23 22 21 8\n");
System.out.println("13 12 11 10 9\n");
int[][] squar=new int[5][5];
void print(){
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(squar[i][j]+"\t");
}
System.out.println();
}
}
void generate(){
int dx=1,dy=1,x=0,y=0,_x,_y;
boolean goHorizon=true;
for(int i=1;i<=25;i++){
squar[y][x]=i;
if(goHorizon){
_x=x+dx;
if( _x>4 || _x<0 || squar[y][_x]!=0 ){
goHorizon=false;
dx=-dx;
y+=dy;
}
else{
x=_x;
}
}
else{
_y=y+dy;
if( _y>4 || _y<0 || squar[_y][x]!=0 ){
goHorizon=true;
dy=-dy;
x+=dx;
}
else{
y=_y;
}
}
}
}
public static void main(String[] args){
Test55 test=new Test55();
test.generate();
test.print();
}
}
System.out.println("16 17 18 19 6\n");
System.out.println("15 24 25 20 7\n");
System.out.println("14 23 22 21 8\n");
System.out.println("13 12 11 10 9\n");
--------------------------------------
我靠。新时代的人才-_-#
也许是正确的哦呵呵。。
private static int [,] intArray = new int[5,5]; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
/*
* 打印螺旋矩陣
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
* */
//int [,] intArray = new int[5,5]; //初始化数据0
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
intArray[i,j] = 0;
}
} string str = "右"; //左上右下
int m = 0;
int n = 0;//当前的位置下标
for(int i=1;i<=25;i++)
{
//左
zuo:if(str == "左")
{
if(IsOk(m,n))
{
intArray[m,n] = i;
n--;
}
else
{
m--;
n++;
str = "上";
}
} //上
if(str == "上")
{
if(IsOk(m,n))
{
intArray[m,n] = i;
m--;
}
else
{
m++;
n++;
str = "右";
}
} //右
if(str == "右")
{
if(IsOk(m,n))
{
intArray[m,n] = i;
n++;
}
else
{
m++;
n--;
str = "下";
}
} //下
if(str == "下")
{
if(IsOk(m,n))
{
intArray[m,n] = i;
m++;
}
else
{
n--;
m--;
str = "左";
goto zuo;
}
}
} //打印数据
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
Response.Write(intArray[i,j].ToString() + " ");
}
Response.Write("<br>");
}
} //试试可不可以写入新的数值
private bool IsOk(int j,int k)
{
//超介或对应的位置不为0,返回false,否则return true;
if(j<0 || 4<j)
{
return false;
}
if(k<0 || 4<k)
{
return false;
}
if(intArray[j,k]==0)
{
return true;
}
else
{
return false;
}
}
const
MAX = 5; //这里是打印6*6的,就是说MAX+1的矩阵,可以改变这个数据来实现其他的var
Mat : array [0..MAX, 0..MAX] of integer;procedure TForm1.PrintMat;
var
Number : Integer; TmpStr : String;
i, j : Integer;
begin
Number := 1; for i := 0 to MAX div 2 + 1 do
begin
//right
for j := i to MAX - i do
begin
Mat[i][j] := Number;
inc ( Number );
end; //down
for j := i + 1 to MAX - i do
begin
Mat[j][MAX-i] := Number;
inc ( Number );
end; //left
for j := MAX - i - 1 downto i+1 do
begin
Mat[MAX - i][j] := Number;
inc ( Number );
end; //up
for j := MAX - i downto i+1 do
begin
Mat[j][i] := Number;
inc ( Number );
end;
end; Memo1.clear;
//Output the mat
for i := 0 to MAX do
begin
TmpStr := '';
for j := 0 to MAX do
TmpStr := TmpStr + IntToStr ( Mat[i][j] ) + ' ';
Memo1.Lines.Add ( TmpStr );
end;
end;
可以参考下
using namespace std;
//打印2维树组
template<typename T>
//nHSize表示有几行,nVSize表示有几列
void Print2DimensionArray( T* pData, int nHSize, int nVSize )
{
for( int i=0;i<nHSize;i++)
{
for( int j=0;j<nVSize;j++)
{
cout<<pData[i*nVSize+j]<<'\t';
}
cout<<endl;
}
}
int TurnDir( int x,int xIncr, int xmin,int xmax )
{
if( xIncr == 0 )
return 0;
else if( x+xIncr < xmin )
return -1;
else if( x+xIncr >xmax )
return 1;
else return 0;
}
//nHSize表示有几行,nVSize表示有几列
void PrintPhalanx( int nHSize ,int nVSize )
{
int size=nHSize*nVSize;
int* pData=new int[size];
int nHmin = 0,nHmax=nHSize-1; //没有signed的行的范围
int nVmin = 0,nVmax=nVSize-1; //没有signed的列的范围
int xIncr=1,yIncr=0; //x,y方向上应该增加的
int x=0,y=0; //数据的坐标
pData[x*nVSize+y]=1;
int isTurn=0;
for( int n=2;n<=size;n++)
{
if(isTurn=TurnDir( x,xIncr,nVmin,nVmax ))
{
xIncr=0;
yIncr=isTurn;
(isTurn>0?nHmin:nHmax )+=isTurn;
}
else if(isTurn=TurnDir(y,yIncr,nHmin,nHmax))
{
yIncr=0;
xIncr=-isTurn;
(isTurn>0?nVmax:nVmin )-=isTurn;
}
x+=xIncr;
y+=yIncr; pData[y*nVSize+x]=n;
}
Print2DimensionArray( pData,nHSize,nVSize );
delete pData;
}
#define ELEMENTCOUNT 8using namespace std;enum Direction{Right,Down,Left,Up};
void ChangeDirection(Direction &direction)
{
if(direction == Right)
direction = Down;
else if(direction == Down)
direction = Left;
else if(direction == Left)
direction = Up;
else
direction = Right;
}
void NextStep(Direction direction,int &xIndex,int &yIndex)
{
if(direction == Right)
yIndex++;
else if(direction == Down)
xIndex++;
else if(direction == Left)
yIndex--;
else
xIndex--;
}
main()
{
int valueArray[ELEMENTCOUNT][ELEMENTCOUNT];
Direction direction=Right;
int xIndex = 0;
int yIndex = 0;
for(int i = 0; i<ELEMENTCOUNT ; i++)
{
for(int j=0;j<ELEMENTCOUNT;j++)
{
valueArray[i][j] =0;
}
}
for(int i =0 ; i<ELEMENTCOUNT*ELEMENTCOUNT ; i++)
{
valueArray[xIndex][yIndex]=i+1;
if(direction == Right)
{
if((yIndex==(ELEMENTCOUNT-1))||valueArray[xIndex][yIndex+1]!=0)
{
ChangeDirection(direction);
}
}
else if(direction== Down)
{
if((xIndex==(ELEMENTCOUNT-1))||valueArray[xIndex+1][yIndex]!=0)
{
ChangeDirection(direction);
}
}
else if(direction== Left)
{
if((yIndex==0)||valueArray[xIndex][yIndex-1]!=0)
{
ChangeDirection(direction);
}
}
else
{
if((xIndex==0)||valueArray[xIndex-1][yIndex]!=0)
{
ChangeDirection(direction);
}
}
NextStep(direction,xIndex,yIndex);
}
for(int i = 0; i<ELEMENTCOUNT ; i++)
{
for(int j=0;j<ELEMENTCOUNT;j++)
{
cout<<valueArray[i][j]<<" ";
}
cout<<"\n";
}
}
package test;public class SpinSquare {
private int[][] array; private int side;// 正方形边长 private int xOffset;// x方向偏移量 private int yOffset;// y方向偏移量 public SpinSquare(int side) {
array = new int[side][side];
this.side = side;
xOffset = 1;
yOffset = 0;
} public void fill() {
int x = 0, y = 0, xNext, yNext;
for (int i = 1; i <= side * side; i++) {
array[x][y] = i;
xNext = x + xOffset;
yNext = y + yOffset;
if (xNext >= 0 && xNext < side & yNext >= 0 && yNext < side
&& array[xNext][yNext] == 0) {
x = xNext;
y = yNext;
} else if (i < side * side) {
int temp = xOffset;
xOffset = (-1) * yOffset;
yOffset = temp;
i--;
}
} } public void print() {
for (int j = 0; j < side; j++) {
for (int i = 0; i < side; i++) {
if (array[i][j] < 10) {
System.out.print(" " + array[i][j] + " ");
} else {
System.out.print(array[i][j] + " ");
}
}
System.out.println();
}
} public static void main(String[] args) {
SpinSquare square = new SpinSquare(6);
square.fill();
square.print();
}
}
static int len = 2; //每次变化
static int start = 1; //起始
static int now = start-len; //当前值
static int X = 0; //X坐标
static int Y = 0; //Y坐标
static int[][] xy = new int[10][10]; //随便定义大小
static int max = (xy.length*xy[0].length-1)*len+start; //最大数,后面用到
public static void goNextxy() //下一步
{
int i=X,j=Y;
switch(d)
{
case 0: //right
i++;
break;
case 1: //down
j++;
break;
case 2: //left
i--;
break;
case 3: //up
j--;
break;
}
if(i>(xy[0].length-1) || j> (xy.length-1) || i<0 || j<0) //如果超数组边界,就下一步
{
d = (d+1)%4;
goNextxy();
}
else if(i<=(xy[0].length-1) && j<= (xy.length-1)) //如果在边界内且该数组已被赋值,就下一步
{
if(xy[j][i]!=0)
{
d = (d+1)%4;
goNextxy();
}
else
{
X = i;
Y = j;
}
}
else
{
X = i;
Y = j;
}
}
public static void main(String[] args) throws Exception {
while(xy[Y][X]==0)
{
xy[Y][X] = now += len;
if(now != max) goNextxy();
}
print(xy);
} public static void print(int[][] arr) //打印数组
{
int maxlen = (max+"").length();
String b="";
for(int m=0;m<maxlen-1;m++) b += " ";
System.out.println("-----------------------");
for(int i =0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++) System.out.print((b + arr[i][j]).substring(String.valueOf(arr[i][j]).length()-1)+" ");
System.out.println();
}
System.out.println("-----------------------");
}
}
static int start = 1; //起始
这个不能为小于1,判断是否为空的时候用了 arr[x][y]==0
汗~~
System.out.println(" 1 2 3 4 5\n");
System.out.println("16 17 18 19 6\n");
System.out.println("15 24 25 20 7\n");
System.out.println("14 23 22 21 8\n");
System.out.println("13 12 11 10 9\n");就这样
最简单!什么问题都复杂化
很明显打印的第一行就不对,你都给a[0][0....5]附了两遍值了
所以打印出来的东西根本就没有1~5那几个数
int i=0,j=0,ccnt=0,iway=1,jway=0;
while(ccnt++<=data.length*data[0].length){
data[i][j]=ccnt;
if(i+iway<0||i+iway>=data.length||j+jway<0||j+jway>=data[0].length||data[i+iway][j+jway]!=0){
if(iway==0){
iway=-1*jway;
jway=0;
}else{
jway=iway;
iway=0;
}
}
i+=iway;
j+=jway;
}一个循环,两重判断,问题搞定,换成任意规则数组,一样正常运行,大家来点掌声鼓励一下吧!
iway=-1*jway;
jway=0;
}else{
jway=iway;
iway=0;
}
改成这样不是更好吗??
int temp=iway;
iway=(-1)*jway;
jway=temp;
我在上边的二维上的实现就是这样子的
private static int MATRIX_SIZE = 6; private static boolean[][] MINES = new boolean[MATRIX_SIZE][MATRIX_SIZE];
private static String[][] VALUES = new String[MATRIX_SIZE][MATRIX_SIZE];
private static final int DIR_RIGHT = 0;
private static final int DIR_DOWN = 1;
private static final int DIR_LEFT = 2;
private static final int DIR_UP = 3;
public static void main(String[] args) {
storeMatrix();
System.out.println("");
}
private static void storeMatrix() {
int count = MATRIX_SIZE * MATRIX_SIZE;
int xPoint = -1;
int yPoint = 0;
int pos = 0;
int dir = DIR_RIGHT;
while (pos < count) {
boolean bull = false;
if (dir == DIR_RIGHT) {
xPoint++;
if (isWall(dir, xPoint, yPoint)) {
xPoint--;
dir = turnDir(dir);
bull = true;
}
} else if (dir == DIR_DOWN) {
yPoint++;
if (isWall(dir, xPoint, yPoint)) {
yPoint--;
dir = turnDir(dir);
bull = true;
}
} else if (dir == DIR_LEFT) {
xPoint--;
if (isWall(dir, xPoint, yPoint)) {
xPoint++;
dir = turnDir(dir);
bull = true;
}
} else if (dir == DIR_UP) {
yPoint--;
if (isWall(dir, xPoint, yPoint)) {
yPoint++;
dir = turnDir(dir);
bull = true;
}
}
if (!bull) {
VALUES[yPoint][xPoint] = String.valueOf(pos + 1);
MINES[yPoint][xPoint] = true;
pos++;
}
}
}
private static int turnDir(int srcDir) {
if (srcDir == DIR_RIGHT) {
return DIR_DOWN;
}
if (srcDir == DIR_DOWN) {
return DIR_LEFT;
}
if (srcDir == DIR_LEFT) {
return DIR_UP;
}
if (srcDir == DIR_UP) {
return DIR_RIGHT;
}
return DIR_RIGHT;
}
private static boolean isWall(int srcDir, int xPoint, int yPoint) {
if (srcDir == DIR_RIGHT && (xPoint == MATRIX_SIZE || MINES[yPoint][xPoint])) {
return true;
}
if (srcDir == DIR_DOWN && (yPoint == MATRIX_SIZE || MINES[yPoint][xPoint])) {
return true;
}
if (srcDir == DIR_LEFT && (xPoint == -1 || MINES[yPoint][xPoint])) {
return true;
}
if (srcDir == DIR_UP && ( yPoint == -1 || MINES[yPoint][xPoint])) {
return true;
}
return false;
}
}
#include<iomanip.h>
main()
{
int a,b,p,q,h=1,e=0,f=1;
cout<<"矩阵的行数和列数:";
cin>>a>>b;
p=a;q=b;
int s[30][30];
for(int i=0,j=0;j<b;j++)
{
s[i][j]=h++;
}
j--;
b--;
while(a||b)
{
if(a)
{
for(i++;i<a;i++)
s[i][j]=h++;
a--; i--;
}
if(j>e)
{
for(j--;j>=e;j--)
s[i][j]=h++;
e++; j++;
}
if(i>f)
{
for(i--;i>=f;i--)
s[i][j]=h++;
f++; i++;
}
if(b)
{
for(j++;j<b;j++)
s[i][j]=h++;
b--; j--;
}
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cout<<setw(3)<<s[i][j];
cout<<endl;
}
return 0;
}
没试过 看看吧应该行
int[][] map=new int[5][5];
int maxX=4;
int minX=0;
int maxY=4;
int minY=0;
int size=0;
int forword=1;//1--> 2| 3<-- 0 |
int length=1;
int firstx=0;//上一个位子的值
int firsty=0;//上一个位子的值
map[firstx][firsty]=1;
for(int i=2;i<=25;i++){
int value=i;
System.out.println(forword);
switch(forword % 4){
case 1:{
firstx=firstx+1;
if(firstx+1>maxX){
forword++;
maxX--;
minY++;
}
break;
}
case 2:{
firsty=firsty+1;
if(firsty+1>maxY){
forword++;
maxY--;
}
break;
}
case 3:{
firstx=firstx-1;
if(firstx-1<minX){
forword++;
minX++;
}
break;
}
case 0:{
firsty=firsty-1;
if(firsty-1<minY){
forword++;
minY++;
}
break;
}
}
map[firstx][firsty]=value;
}
for(int i=0;i<5;i++){
System.out.print('\n');
for(int j=0;j<5;j++){
System.out.print(map[j][i]);
System.out.print(" ");
}
}
}
System.out.println(" 1 2 3 4 5");
System.out.println("16 17 18 19 6");
System.out.println("15 24 25 20 7");
System.out.println("14 23 22 21 8");
System.out.println("13 12 11 10 9");
}
}
youngchulli:public class Model { public static void main(String[] args) {
System.out.println(" 1 2 3 4 5");
System.out.println("16 17 18 19 6");
System.out.println("15 24 25 20 7");
System.out.println("14 23 22 21 8");
System.out.println("13 12 11 10 9");
}
}不错!
int[][] num = new int[15][15];
boolean X = true,Y =false,b = X;
int directionx = 1,directiony = 1;
int currentx = 0,currenty = 0;
int count = 0;
boolean end = false;
public test(){}
public test(String s1,String s2){
num = null;
num = new int[Integer.parseInt(s1)][Integer.parseInt(s2)];
}
public void run(){
while(!end){
if(b==X&&!end)
for(;true;currentx+=directionx){
if(((directionx==1)&&((currentx>=num[0].length-1)||((currentx<num[0].length-1)&&(num[currentx+directionx][currenty]!=0))))||(directionx==-1)&&((currentx<=0)||((currentx>0)&&(num[currentx+directionx][currenty]!=0)))){
if(num[currentx][currenty+directiony]!=0)
end = true;
b=Y;
directionx*=-1;
break;
}
num[currentx][currenty] = ++count;
}
if(b==Y&&!end)
for(;true;currenty+=directiony){
if(((directiony==1)&&((currenty>=num.length-1)||((currenty<num.length-1)&&(num[currentx][currenty+directiony]!=0))))||(directiony==-1)&&((currenty<=0)||((currenty>0)&&(num[currentx][currenty+directiony]!=0)))){
if(num[currentx+directionx][currenty]!=0)
end = true;
b=X;
directiony*=-1;
break;
}
num[currentx][currenty] = ++count;
}
}
num[currentx][currenty] = ++count;
for(int i=0;i<num.length;i++){
for(int j=0;j<num[0].length;j++)
System.out.print(num[i][j]+(num[i][j]<10?" ":num[i][j]<100?" ":" "));
System.out.println();
}
}
public static void main(String args[]){
if(args.length==2&&args[0].equals(args[1]))
new test(args[0],args[1]).run();
else
new test().run();
}
}
int getN = 9;
int[] n = new int[getN*getN];
int num = 0;
int Arr = 0;
int i = 0;
int ii=getN-1,jj=1,nn=1; for(i=0;i<getN;i++){
n[i] = ++num;
Arr = i;
}
while(ii>=jj){
for(i=jj;i<=ii;i++){
if(nn%2!=0)
Arr = Arr+getN;
else
Arr = Arr-getN;
n[Arr] = ++num;
}
for(i=jj;i<=ii;i++){
if(nn%2!=0)
Arr = Arr-1;
else
Arr = Arr+1;
n[Arr] = ++num;
}
if(nn%2==0)
jj++;
else
ii--;
nn++;
}
for(i=0;i<getN*getN;i++){
if(n[i]<10)
System.out.print("0"+n[i]+" ");
else
System.out.print(n[i]+" ");
if((i+1)%getN==0){
System.out.println("");
}
}
}
}