锁定老帖子 主题:深圳一家公司面试问题,很囧
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-11
hesy_007 写道
mccxj 写道
n=8 x=[] (0...n).each do |i| x[i]=[] end # 初始化 def set(n,x,k) start = n*n-(n-2*k+2)*(n-2*k+2)+1 # 每个框框的开始值 step = n-2*k+1 # 每个框框的边长 (0..step).each do |i| # 上边 x[k-1][k+i-1]=start+i end (1..step).each do |i| # 右边 x[k+i-1][k+step-1]=start+step+i end (1..step).each do |i| # 下边 x[k+step-1][k+i-2]=start+3*step-i+1 end (1..step-1).each do |i| # 左边 x[k+i-1][k-1]=start+4*step-i end end (1..n).each do |i| set(n,x,i) end # 从外到里计算框框~~ (1..n).each do |i| p x[i-1].join(" ") end # 打印~~ ruby代码 简略草稿版~~
步骤:设置一个数据来保存,把数组看成一个个的正方形,计算每个正方形里边的值,并打印。。其他都是数学了。。 这题就ACM水平了?远着吧~~
这就显摆了? java的语法结构本来就不适合表达算法思路,所以我选择ruby,别好像我不会写java一样~~ |
|
返回顶楼 | |
发表时间:2009-12-11
mccxj 写道
def get(row,col,n) k=(if (2*row-n).abs<(2*col-n).abs then col else row end) k=n+1-k if (n+1)/2<k # 第几个圈 start = n*n-(n-2*k+2)*(n-2*k+2)+1 # 该圈的开始 step = n-2*k+1 # 该圈的边长 if row==k # 在上边 return start+ col - row elsif row==k+step # 在下边 return start+ 2*step + row - col elsif col==k # 在左边 return start + 4*step - (row - col) elsif col==k+step # 在右边 return start + 2*step - (col - row) end end 意犹未尽~~直接找出某点的数字的算法~~纯属数学逻辑了~~ ps : 自我感觉,用java来描述算法太蹩脚~~ 用java怎么就蹩脚了,代码多点而已。 |
|
返回顶楼 | |
发表时间:2009-12-11
哎。 大学教科书上的题目。
|
|
返回顶楼 | |
发表时间:2009-12-11
private static int countTotal = 0;
private static int size; public static void main(String[] args) { size = 11; aroundCricle(size); } /** * */ public static void aroundCricle(int size) { countTotal = size * size; int n = 1; int arr[][] = new int[size][size]; for (int i = 0; i < size; i++) { Arrays.fill(arr[i], 0); } settingData(n, 0, 0, arr, 1); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print((arr[i][j]<10?"0"+arr[i][j]:arr[i][j])); System.out.print(" "); } System.out.println(); } } public static void settingData(int n, int x, int y, int arrInt[][], int count) { switch (n % 4) { case 1: for (int j = x; j < size - n / 4; j++) { arrInt[y][j] = count++; if (j == size - n / 4 - 1) { n++; x = y + 1; y = j; break; } } if (count == countTotal + 1) { return; } case 2: for (int j = x; j < size - n / 4; j++) { arrInt[j][y] = count++; if (j == size - n / 4 - 1) { n++; x = y - 1; y = j; break; } } if (count == countTotal + 1) { return; } case 3: for (int j = x; j >= n / 4; j--) { arrInt[y][j] = count++; if (j == n / 4) { n++; x = y - 1; y = j; break; } } if (count == countTotal + 1) { return; } default: for (int j = x; j >= n / 4; j--) { arrInt[j][y] = count++; if (j == n / 4) { n++; x = y + 1; y = j; break; } } if (count == countTotal + 1) { return; } } settingData(n, x, y, arrInt, count); } |
|
返回顶楼 | |
发表时间:2009-12-11
发一个我刚刚写的。有些麻烦,是最基本的while循环。
public static void firstMethod(int i){ if(i>0){ int[][] intarray=new int[i][i]; int x=0,y=0; int num=1; int mx=i,my=i; int nx=0,ny=0; boolean run=true; boolean east=true; boolean west=false; boolean south=false; boolean north=false; while(run){ if(east&&(y<=my)){ if(y==my){ y--; nx++; x++; east=false; south=true; }else if(intarray[x][y]==0){ intarray[x][y]=num; y++; num++; } }else if(west&&(y>=ny)){ if(y==ny){ intarray[x][y]=num; num++; mx--; x--; west=false; north=true; }else if(intarray[x][y]==0){ intarray[x][y]=num; y--; num++; } }else if(south&&(x<=mx)){ if(x==mx){ x--; my--; y--; south=false; west=true; }else if(intarray[x][y]==0){ intarray[x][y]=num; x++; num++; } }else if(north&&(x>=nx)){ if(x==nx){ intarray[x][y]=num; num++; ny++; y++; north=false; east=true; }else if(intarray[x][y]==0){ intarray[x][y]=num; x--; num++; } }else{ run=false; } } for(int k=0;k<i;k++){ for(int l=0;l<i;l++){ if(intarray[k][l]<10){ System.out.print(" "+intarray[k][l]); }else{ System.out.print(" "+intarray[k][l]); } } System.out.println(""); } } } |
|
返回顶楼 | |
发表时间:2009-12-11
C的练习题,打印字符直接定位的。
|
|
返回顶楼 | |
发表时间:2009-12-11
我想用个二维数组 ,应该很容易就搞定了。
|
|
返回顶楼 | |
发表时间:2009-12-11
最后修改:2009-12-11
最近在学PYTHON,就试着写了一下。。。代码有点难看,有不少冗余也懒的重构的。
不过还好,还能跑~有兴趣的朋友可以跑一下。。。 矩阵的边长用side变量表示,修改side可以看效果,不过如果数字超过3位数要修改下打印输出 def print_matrix(matrix): for line in matrix: for i in line: print ("%2s")%i, print #change the side side = 6; startCol = 0; endCol = side; startRow = 0; endRow = side; matrix = [[0 for column in range(side)] for row in range(side)] north = 0 east = 1 south = 2 west = 3 direction = north i=0 j=0 number = 1; while startRow<endRow and startCol<endCol: if direction==north: for j in range(startRow,endRow): matrix[i][j] = number number = number + 1 direction = (direction + 1)%4 startCol = startCol +1 elif direction==east: for i in range(startCol,endCol): matrix[i][j] = number number = number + 1 direction = (direction + 1)%4 endRow = endRow -1 elif direction==south: tmp = range(startRow,endRow) tmp.reverse() for j in tmp: matrix[i][j] = number number = number + 1 direction = (direction + 1)%4 endCol = endCol -1 elif direction==west: tmp = range(startCol,endCol) tmp.reverse() for i in tmp: matrix[i][j] = number number = number + 1 direction = (direction + 1)%4 startRow = startRow + 1 print_matrix(matrix) |
|
返回顶楼 | |
发表时间:2009-12-11
caiwenhn2008 写道 这个恶心的题貌似原来学c语言见过, 我来贴一个别人解决方案,转化为二维数组初始化问题。
非常赞同这种解法 , 接近正常人的思维,不需要任何理论,关键是解题思路,不是代码。 算法简单出bug的机会也少。 |
|
返回顶楼 | |
发表时间:2009-12-11
public class Snake { public static void print(int dimension){ //计算层数 int level = 0; if(dimension%2==0) level = dimension/2; else level = (dimension+1)/2; //初始化数组 int[][] array = initArray(dimension); //重新设置数组中的值 for(int i=0;i<level;i++){ resetArrayValueByLevel(i,array,dimension-1); } for(int i=0;i<dimension;i++){ for(int j=0;j<dimension;j++){ System.out.print(array[i][j]+" "); } System.out.println(); } } public static void resetArrayValueByLevel(int level,int[][] array,int dimension){ int maxTop = resetTopValue(level,dimension,getStartNumberByLevel(level,dimension),array); int maxRight = resetRightValue(level,dimension,maxTop,array); int maxBottom = resetBottomValue(level,dimension,maxRight,array); resetLeftValue(level,dimension,maxBottom,array); } //设置当前层的上侧一条边的值 public static int resetTopValue(int level,int dimension,int startValue,int[][] array){ for(int i=level ; i<=dimension-level ; i++){ array[level][i] = startValue + i-level; } return array[level][dimension-level]; } //设置当前层的右侧一条边的值 public static int resetRightValue(int level,int dimension,int startValue,int[][] array){ for(int i=level ; i<=dimension-level ; i++){ array[i][dimension-level]=startValue + i-level; } int row = level + getDimensionByLevel(level,dimension) - 1; return array[row][dimension-level]; } //设置当前层的下侧一条边的值 public static int resetBottomValue(int level,int dimension,int startValue,int[][] array){ int row = level + getDimensionByLevel(level,dimension) - 1; for(int i=dimension-level ; i>=level ; i--){ array[row][i]=startValue - i + row; } return array[row][level]; } //设置当前层的左侧一条边的值 public static void resetLeftValue(int level,int dimension,int startValue,int[][] array){ int row = level + getDimensionByLevel(level,dimension) - 1; for(int i=row ; i>level ; i--){ array[i][level] = startValue - i + row; } } //取得当前层的开始数 public static int getStartNumberByLevel(int level,int dimension){ return getMaxNumberOfPreLevel(level-1,dimension) + 1; } //取得前一层的最大数 public static int getMaxNumberOfPreLevel(int level,int dimension){ int sum = 0; for(int i=0;i<=level;i++){ sum+=getSumNumberCount(i,dimension); } return sum; } //统计某一层数字的个数 public static int getSumNumberCount(int level,int dimension){ return getDimensionByLevel(level,dimension)*4-4; } //取得每一层数组的维数 public static int getDimensionByLevel(int level,int dimension){ int startD = level; int endD = dimension - level; int len = endD - startD + 1; return len; } public static int[][] initArray(int i){ return new int[i][i]; } public static void main(String[] args) { Snake.print(6); } } |
|
返回顶楼 | |