发表时间:2010-05-27
cocoa2135 写道
int i=5; 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 i=6 System.out.println("1 2 3 4 5 6"); System.out.println("20 21 22 23 24 7"); System.out.println("19 32 33 34 25 8"); System.out.println("18 31 36 35 26 9"); System.out.println("17 30 29 28 27 10"); System.out.println("16 15 14 13 12 11"); 两个字:弓,虽,这是我看过最简洁的算法了
|
|
发表时间:2010-06-01
caiwenhn2008 写道 贴一个可以跑的
class snakePrint { static int length = 7; static int value = 1; static int[][] snake = new int[length][length]; static Direction lastDirection = Direction.Right; static enum Direction { Right, Down, Left, Up; } public static void initialArray() { int row = 0, line = 0; for (int c = 0; c < length * length; c++) { snake[row][line] = value; lastDirection = findDirection(row, line); switch (lastDirection) { case Right: line++; break; case Down: row++; break; case Left: line--; break; case Up: row--; break; default: System.out.println("error"); } value++; } } static Direction findDirection(int row, int line) { Direction direction = lastDirection; switch (direction) { case Right: { if ((line == length - 1) || (snake[row][line + 1] != 0)) direction = direction.Down; break; } case Down: { if ((row == length - 1) || (snake[row + 1][line] != 0)) direction = direction.Left; break; } case Left: { if ((line == 0) || (snake[row][line - 1] != 0)) direction = direction.Up; break; } case Up: { if (snake[row - 1][line] != 0) direction = direction.Right; break; } } return direction; } public static void main(String[] args) { initialArray(); // display..... for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { System.out.print(snake[i][j] + " "); } System.out.println(); } } } 我这边跑不起来,数组越界…… |
|
发表时间:2010-07-02
第七页的那个用一维数组实现的高手厉害
|
|
发表时间:2010-07-10
魔法方阵!
|
|
发表时间:2010-07-12
package com.manson;
public class section1 { static int N =20; static int value = 1; static int[][] result = new int[N][N]; public static void main(String[] args) { int s=0; //竖列值 int h=N-1; //横列值 while( h-s > -1) { setH(s,s,h,0); //图形上面一行 setS(s+1,h,h,0); //图形右面一行 setH(h,s,h-1,1);//图形下面一行 setS(s+1,h-1,s,1); //图形左面一行 s++;h--; } getArr(); } //竖行往里面塞值 public static void setH(int s,int beg,int end,int flag) { if(flag==0) for(int i=beg;i<=end;i++) result[s][i]=value++; if(flag==1) for(int i=end;i>=beg;i--) result[s][i]=value++; } //横行往里面塞值 public static void setS(int beg,int end,int h,int flag) { if(flag==0) for(int i=beg;i<=end;i++) result[i][h]=value++; if(flag==1) for(int i=end;i>=beg;i--) result[i][h]=value++; } //获取数组值 public static void getArr() { for(int i=0;i<result.length;i++) { for(int j=0;j<result[i].length;j++) { System.out.print(result[i][j]+" "); } System.out.println(""); } } } |
|
发表时间:2010-07-12
package com.manson; public class section1 { static int N =20; static int value = 1; static int[][] result = new int[N][N]; public static void main(String[] args) { int s=0; //竖列值 int h=N-1; //横列值 while( h-s > -1) { setH(s,s,h,0); //图形上面一行 setS(s+1,h,h,0); //图形右面一行 setH(h,s,h-1,1);//图形下面一行 setS(s+1,h-1,s,1); //图形左面一行 s++;h--; } getArr(); } //竖行往里面塞值 public static void setH(int s,int beg,int end,int flag) { if(flag==0) for(int i=beg;i<=end;i++) result[s][i]=value++; if(flag==1) for(int i=end;i>=beg;i--) result[s][i]=value++; } //横行往里面塞值 public static void setS(int beg,int end,int h,int flag) { if(flag==0) for(int i=beg;i<=end;i++) result[i][h]=value++; if(flag==1) for(int i=end;i>=beg;i--) result[i][h]=value++; } //获取数组值 public static void getArr() { for(int i=0;i<result.length;i++) { for(int j=0;j<result[i].length;j++) { System.out.print(result[i][j]+" "); } System.out.println(""); } } } |
|
发表时间:2010-07-12
这个好像是润和的面试题
|
|
发表时间:2010-07-12
public class Test { |
|
发表时间:2010-08-12
写完下面的代码后看了其它的帖子,同样问题解决办法真是五花八门,几乎没重样的,哈哈哈!这也许就是数学的魅力吧!俺说说俺的数学模型:
每个循环的规律如下: i循环次数; len为二维数组最大索引 向右:(i,i到(len-i)) x=i,y=i to y=len-i y++ 向下:((i+1)到(len-i),len-i) x=i+1 to x=len-i, y =len-i x++ 向左:((len-i),(len-(i+1))到i) x=len-i, y=len-(i+1) to y=i y-- 向上:((len-(i+1))到i,i)(注意这里不包含i) x=len-(i+1) to x = i,y=i x-- x<i public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //double A = 19 % 6.7; //System.out.println(A); //行列数 int length = 9; //二维数组 int[][] data = new int[length][length]; //总数 int total = length*length; //数组长度最大索引 int sumLine = length -1 ; //数组被2整除的余数 int loopYu = length%2; //循环次数(四舍五入) int loopNub = length/2; //如果余数为0循环次数+1 if(loopYu == 0){ loopNub = length/2+1; } //计数 int count = 0; for(int i=0;i<=loopNub;i++){ //向右计数 for(int y=i;y<=sumLine-i;y++){ count++; data[i][y] = count; if(count == total){ System.out.println("break right!"); break; } } //向下计数 for(int x =i+1;x<=sumLine-i;x++){ count++; data[x][sumLine-i] = count; if(count == total){ System.out.println("break down!"); break; } } //向左计数 for(int y = sumLine-(i+1);y>=i;y--){ count++; data[sumLine-i][y] = count; if(count == total){ System.out.println("break left!"); break; } } //向上计数// 注意这里不包含i for(int x = sumLine-(i+1);x>i;x--){ count++; data[x][i]=count; if(count == total){ System.out.println("break up!"); break; } } } //显示数组内容 for(int i = 0;i<length;i++){ for(int j=0; j<length ;j++){ System.out.print(String.format("%02d",data[i][j])+ " "); } System.out.println( ); } } } |
|
发表时间:2010-08-13
改造后的任意维数的算法及代码:
public class TestXy { /** * 数据模型 * * i为循环的次数为行数除2后取整,如果整除刚结果加1 * lenX数组行数最大索引 * lenY数组列数最大索引 * 我们可以得出每层的算法规律如下: * 向右-》X坐标:i;Y坐标:从i递增到lenY-i step=1 * 向下-》X坐标:从i+1递增到lenX-i step=1;Y坐标:lenY-i * 向左-》X坐标:lenX-i;Y坐标:从leny-(i+1)递减到i * 向上-》Y坐标:从lenX-(i+1)递减到i+1;Y坐标:i */ /** * @param xx * 输入行数 * @param yy * 输入列数 */ private static long[][] getDataByLengthXy(int xx,int yy){ int lenx = xx -1; int leny = yy -1; //二维数组 long[][] data = new long[xx][yy]; //总数 long total = xx*yy; //数组长度最大索引 int sumLine = xx -1 ; //数组被2整除的余数 int loopYu = xx%2; //循环次数(四舍五入) int loopNub = xx/2; //如果余数为0循环次数+1 if(loopYu == 0){ loopNub = xx/2+1; } //计数 long count = 0; for(int i=0;i<=loopNub;i++){ //向右计数 for(int y=i;y<=leny-i;y++){ count++; data[i][y] = count; if(count == total){ return data; } } //向下计数 for(int x =i+1;x<=lenx-i;x++){ count++; data[x][leny-i] = count; if(count == total){ //System.out.println("break down!"); return data; } } //向左计数 for(int y = leny-(i+1);y>=i;y--){ count++; data[lenx-i][y] = count; if(count == total){ //System.out.println("break left!"); return data; } } //向上计数 for(int x = lenx-(i+1);x>=i+1;x--){ count++; data[x][i]=count; if(count == total){ //System.out.println("break up!"); return data; } } } return data; } public static void main(String[] args) { // TODO Auto-generated method stub long startTime=System.currentTimeMillis(); int xx =10; int yy =5; long data[][] = getDataByLengthXy(xx,yy); System.out.println("DONE! "+(System.currentTimeMillis()-startTime)+"ms"); //显示数组内容 for(int i = 0;i<xx;i++){ for(int j=0; j<yy ;j++){ System.out.print(String.format("%05d",data[i][j])+ " "); } System.out.println( ); } System.out.println("DONE! "+(System.currentTimeMillis()-startTime)+"ms"); } } |