锁定老帖子 主题:深圳一家公司面试问题,很囧
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-04-11
最后修改:2010-04-11
javascript版本,可直接粘贴到chrome控制台下运行,
二维数组方式实现最后是一行一行打印的; 支持顺时针和逆时针旋转; 不大喜欢递归,时间空间开销都大,而且还不易修改,算位置的话就纯数学了,我的数学不大好 (function(){ var RIGHT = 0, DOWN = 1, LEFT = 2, UP = 3;//闭包常量 //初始化参数 positonManager = function(length,clockwise){ this.length = length; this.clockwise=clockwise; this.cx = 0;//当前x坐标 this.cy = 0;//当前y坐标 this.cn = 1;//当前数值 this.cm = clockwise?RIGHT:DOWN;//当前移动方向 this.positions = new Array(length);//空的二维数组 for (var i = 0; i < length; i++) { this.positions[i] = new Array(length); } this.positions[0][0] = ' 1'; } positonManager.prototype = { moveNext: function(){ if (this.addNewNum(this.cm)) {//继续上次方向 return true; } else if (this.addNewNum((this.cm + (this.clockwise?1:3))%4)) {//因为是个螺旋,转向只能按照固定次序进行循环! return true; } return false;//无路可走了! }, //根据方向获取下一地点后判断是否已占位,未占位则赋值,已占位返回false addNewNum: function(m){ var xy = this.getNewPosition(m); if (xy !== false && this.positions[xy[1]][xy[0]] == undefined) { this.cn++; this.positions[xy[1]][xy[0]] = this.cn < 10 ? ' ' + this.cn : '' + this.cn; this.cx = xy[0]; this.cy = xy[1]; this.cm = m; return true; } return false; }, //根据方向获取下一地点,越界返回false getNewPosition: function(dir){ switch (dir) { case RIGHT: return (this.cx + 1 == this.length ? false : [this.cx + 1, this.cy]); case DOWN: return (this.cy + 1 == this.length ? false : [this.cx, this.cy + 1]); case LEFT: return (this.cx == 0 ? false : [this.cx - 1, this.cy]); case UP: return (this.cy == 0 ? false : [this.cx, this.cy - 1]); } }, printAll: function(){ var i, j,line; while (this.moveNext()){} for (i=0; i < this.length; i++) { line=''; for (j=0; j < this.length; j++) { line+=this.positions[i][j]+' '; } console.log(line); } } } })() console.log('int i=5;'); var thread1 = new positonManager(5,false); thread1.printAll(); console.log('int i=6;'); var thread = new positonManager(6,true); thread.printAll(); |
|
返回顶楼 | |
发表时间:2010-04-12
小改一下,上面的显示问题
public static void main(String[] args) { initialArray(); // display..... for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { System.out.printf("%4d",snake[i][j]); //只有这一处,改了好看点。 } System.out.println(); } } |
|
返回顶楼 | |
发表时间:2010-04-13
最后修改:2010-04-13
package 回旋矩阵; public class SnakeMyWay_ok { /** * @param args */ public static void main(String[] args) { for(int i=1;i<7;i++){ System.out.println("边长="+i); printData(fillData(i, i)); System.out.println(); } } private static int[][] fillData(int width, int height) { if(width<1 || height<1){ throw new IllegalArgumentException("参数高宽必须大于0"); } int[][] array = new int[height][width]; int pointValue = 1; int pointMaxValue = width * height; int x = 0; int y = 0; //右、下、左、上的四个方向的x、y方向的步进 int[][] xySteps = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; // y,x //初始方向为右 int currentXyStepType = 0; while (pointValue <= pointMaxValue) { array[y][x] = pointValue; //1.a.当前x、y步进 int yStep = xySteps[currentXyStepType][0]; int xStep = xySteps[currentXyStepType][1]; //1.b.按当前方向前进下点的坐标 int newx = x + xStep; int newy = y + yStep; //1.c.检查坐标是否越界或者已经被占,如果是则转向 if (newx > width - 1 || newx < 0 || newy > height - 1 || newy < 0 || array[newy][newx] > 0) { currentXyStepType++; if (currentXyStepType > xySteps.length - 1) { currentXyStepType = 0; } } //2.按正确的方向前进 y += xySteps[currentXyStepType][0]; x += xySteps[currentXyStepType][1]; pointValue++; } return array; } private static void printData(int[][] array) { if (array.length > 0 && array[0].length > 0) { int charlen = String.valueOf(array.length * array[0].length) .length() + 1; for (int y = 0; y < array.length; y++) { for (int w = 0; w < array[0].length; w++) { System.out.printf("%" + charlen + "d", array[y][w]); } System.out.println(); } } } } |
|
返回顶楼 | |
发表时间:2010-04-15
最后修改:2010-04-16
首先声明,没参考任何理论资料和其他人的算法。如果雷同,纯属巧合!
我用的一种方法,不论多大的边长值,只用八次循环。由于在单位,注解随后补上!见谅! public class CircleMatrix{ private final static int[] oddLenAngleX={-1, -1, 0, 1, 1, 1, 0, -1}; private final static int[] oddLenAngleY={0, 1, 1, 1, 0, -1, -1, -1}; private final static int[] evenLenAngleX={1, 1, 0, -1, -1, -1, 0, 1}; private final static int[] evenLenAngleY={0, -1, -1, -1, 0, 1, 1, 1}; private static int[][] drawNumMatrix(int len, int[] xangle, int[] yangle){ int coreToSideDist=0, positionX=0, positionY=0, coreNum=0, number=0, prevStep=0, flankSize=0, coreX=0, coreY=0, matrix[][]; coreToSideDist=len/2; coreNum=len*len; if(len%2==0) { coreX=len/2; coreY=len/2+1; } else { coreX=len/2+1; coreY=len/2+1; } matrix=new int[len][len]; matrix[coreX-1][coreY-1]=coreNum; //System.out.println("coreToSideDist="+coreToSideDist+" "+"coreNum="+coreNum+" "+ "coreX"+coreX+" "+"coreY="+coreY); for(int i=1; i<=8; i++) { //System.out.println("NEW DIRECTION"); number=coreNum; for(int d=1; d<=coreToSideDist; d++) { if(1==d) prevStep=i; else prevStep=prevStep+8; //System.out.println("number: "+number+"-"+prevStep+" = "+(number-prevStep)); number=number-prevStep; if(number<=0) continue; positionX=coreX+d*xangle[i-1]-1; positionY=coreY+d*yangle[i-1]-1; //System.out.println("["+xangle[i-1]+","+yangle[i-1]+"]"); //System.out.println("["+positionX+","+positionY+"] = "+number); matrix[positionX][positionY]=number; flankSize=d-1; if(0==yangle[i-1]) { for(int s=1; s<=flankSize; s++) { matrix[positionX][positionY+s*xangle[i-1]]=number+s; matrix[positionX][positionY-s*xangle[i-1]]=number-s; //System.out.println("["+positionX+","+(positionY+s*xangle[i-1])+"] = "+(number+s)); //System.out.println("["+positionX+","+(positionY-s*xangle[i-1])+"] = "+(number-s)); } } if(0==xangle[i-1]) { for(int s=1; s<=flankSize; s++) { matrix[positionX+s*yangle[i-1]][positionY]=number-s; matrix[positionX-s*yangle[i-1]][positionY]=number+s; //System.out.println("["+(positionX+s*yangle[i-1])+","+positionY+"] = "+(number-s)); //System.out.println("["+(positionX-s*yangle[i-1])+","+positionY+"] = "+(number+s)); } } } } return matrix; } public static void main(String[] args){ int length=10; int matrix[][]; if(length%2==0) matrix=drawNumMatrix(length, evenLenAngleX, evenLenAngleY); else matrix=drawNumMatrix(length, oddLenAngleX, oddLenAngleY); long startTime=System.currentTimeMillis(); for(int y=0; y<length; y++) { for(int x=0; x<length; x++) { System.out.print(matrix[x][y]+"\t"); } System.out.print("\n"); } System.out.println("DONE! "+(System.currentTimeMillis()-startTime)+"ms"); } } |
|
返回顶楼 | |
发表时间:2010-05-11
看看我的有没有问题:
package practice public class CircleArray { private int[][] array = null; private int maxInt; private int width; private static final int right = 0; private static final int down = 1; private static final int left = 2; private static final int up = 3; public CircleArray(int width) { this.width = width; array = new int[width][width]; maxInt = width * width; drawArray(array); } public void drawArray(int[][] array) { int direction = 0; int row = 0; int column = 0; for (int value = 1; value <= maxInt; value++) { array[row][column] = value; switch (direction) { case right: { column++; if (!canDraw(array, row, column)) { column--; row++; direction=down; } break; } case left: { column--; if (!canDraw(array, row, column)) { column++; row--; direction=up; } break; } case up: { row--; if (!canDraw(array, row, column)) { row++; column++; direction=right; } break; } case down: { row++; if (!canDraw(array, row, column)) { row--; column--; direction=left; } break; } default: break; } } } private boolean canDraw(int[][] array, int i, int j) { if (i >= width || j >= width||i<0||j<0) return false; if (array[i][j] != 0) return false; return true; } public void printArray() { for (int i = 0; i < array.length; i++) for (int j = 0; j < array[i].length; j++) if(j == array[i].length - 1) System.out.print(array[i][j] + "\n"); else System.out.print(array[i][j]>=10?array[i][j]+" ":array[i][j]+" "); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub CircleArray array=new CircleArray (6); array.printArray(); } } |
|
返回顶楼 | |
发表时间:2010-05-17
最后修改:2010-05-17
凑个热闹: function build(n){ function get(x,y){ var p1 = y,p2=n-x-1,p3=n-y-1;p4=x; var sum = 0; var min = Math.min(p1,p2,p3,p4); if(p1==min){ sum+=p4-min; }else{ var len = n-min*2; sum+=len; if(p2 == min){ sum+=p1-min-1; }else{ sum+=len-1; if(p3 == min){ sum+=p2-min-1; }else{ sum+=len-1; sum+=p3-min-1; } } } while(min--){ sum+=((n-min*2)*4-4); } return sum; } var buf = []; for(var y=0;y<n;y++){ for(var x=0;x<n;x++){ buf.push(get(x,y)) buf.push("\t") } buf.push("\n") } return buf; } alert(build(8).join(''))
|
|
返回顶楼 | |
发表时间:2010-05-17
貌似螺旋数~
|
|
返回顶楼 | |
发表时间:2010-05-18
最后修改:2010-05-18
private boolean doInsertIntoArray(int col, int row ,int count,int a, int[][] result) { boolean isSingle = a % 2 != 0 && result[a / 2][a / 2] == 0; boolean isDouble = a % 2 == 0 && result[(a / 2) + 1][(a / 2) + 1] == 0; for(int t = 0 ;t<a ;t++){ result[row][col] = ++count; col++; } col--; for(int t = 0 ;t<a-1 ;t++){ row++; result[row][col] = ++count; } for(int t = 0 ;t<a-1 ;t++){ col--; result[row][col] = ++count; } for(int t = 0 ;t<a-2 ;t++){ row--; result[row][col] = ++count; } if (isSingle ||isDouble) { doInsertIntoArray(++col,row,count,a-2,result); } return true; } |
|
返回顶楼 | |
发表时间:2010-05-18
递归的代码。一开始想是用递归比较直观,写完发现不用递归也很直观 。
public class SnakePrint { public static void initialArray(int beginIndex, int beginNum, int n, int[][] data) { if(n <= 0) { return; } initialBorder(beginIndex, beginNum, n, data); initialArray(beginIndex + 1, beginNum + (n-1)*4 , n - 2, data); } private static void initialBorder(int beginIndex, int beginNum, int n, int[][] data) { if(n == 1) { data[beginIndex][beginIndex] = beginNum; return; } for(int j = 0; j < n-1; j++) { //top data[beginIndex][beginIndex + j] = beginNum + j; //right data[beginIndex + j][beginIndex + (n-1)] = beginNum + n-1 + j; //bottom data[beginIndex + (n-1)][beginIndex + (n-1) - j] = beginNum + 2 * (n-1) + j; //right data[beginIndex + (n-1) - j][beginIndex] = beginNum + 3 * (n-1) + j; } } public static void main(String[] args) { int size = 50; int[][] data = new int[size][size]; initialArray(0, 1 , size, data); for(int i = 0; i < size; i++) { for(int j = 0; j < size; j++) { System.out.print(data[i][j] + " "); } System.out.println(); } } } |
|
返回顶楼 | |
发表时间:2010-05-18
使用向量的方法,Delphi代码(感觉自己像外星人)
var i,j, n: Integer; oArray: TIntegerDynArrayArray; oPoint, oTempPoint: TGGLPoint2d; oVec: TGGLVector2d; begin n := 9; SetLength(oArray, n); for I := 0 to n - 1 do SetLength(oArray[i], n); //初始化数组 oPoint := Point2d(-1, 0); oVec := Point2d(1, 0); i := 1; while True do begin if i > n * n then Break; oTempPoint := oPoint + oVec; if ((oTempPoint.X = n) or (oTempPoint.Y = n)) or (oArray[Trunc(oTempPoint.Y)][Trunc(oTempPoint.X)] <> 0 ) then oVec := oVec.RotateHalfPi(False) else begin oArray[Trunc(oTempPoint.Y)][Trunc(oTempPoint.X)] := i; oPoint := oTempPoint; Inc(i); end; end; end; |
|
返回顶楼 | |