锁定老帖子 主题:深圳一家公司面试问题,很囧
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-28
def print_n(n):
r = [n*[None] for i in range(n)] xy = [0, 0] d = 1 s = 1 for i in range(1, 1+n*n): r[xy[0]][xy[1]] = i m = xy[:] m[d] += s if m[d] >= n or r[m[0]][m[1]] is not None: if d == 1: d = 0 else: s = -s d = 1 xy[d] += s print "\n".join(" ".join(map(lambda x:"%4s"%x, i)) for i in r) print_n(5) print_n(25) |
|
返回顶楼 | |
发表时间:2009-12-30
发一个比较短的Python版本
MAX = 10 matrix = [[0 for col in range(MAX)] for row in range(MAX)] (x, y, count, link) = (-1, 0, 1, {1:(1,0,2), 2:(0,1,3), 3:(-1,0,4), 4:(0,-1,1)}) (dx, dy, direct) = link[1] while count <= MAX*MAX: (nx, ny) = (x + dx, y + dy) if (0 <= nx < MAX) and (0 <= ny < MAX) and (matrix[ny][nx] == 0): matrix[ny][nx] = count (x, y, count) = (nx, ny, count + 1) else: (dx, dy, direct) = link[direct] for x in range(MAX): print matrix[x] |
|
返回顶楼 | |
发表时间:2009-12-30
//出一个符合人类思维的最傻的方法
int[][] arr=new int[size+2][size+2];//生产一个大一点的数组 for(int i=0;i<size+2;i++){//建造墙壁 arr[0][i]=-1; arr[size+1][i]=-1; arr[i][0]=-1; arr[i][size+1]=-1; } int[] p=new int[]{1,1}; int d=1; int step=1; int num=1; while(true){ arr[p[0]][p[1]]=num;//给值 num++; p[d]=p[d]+step; if(arr[p[0]][p[1]] !=0){//判断是否撞墙 p[d]=p[d]-step;//后退 if(d==0) step=-step;//转向 d=(d+1)%2; p[d]=p[d]+step; if(arr[p[0]][p[1]] !=0) break;//再次撞墙就退出 } } for(int i=0;i<size;i++){ for(int j=0;j<size;j++){ //ret[i][j]=arr[i+1][j+1]; System.out.print(arr[i+1][j+1]+"\t"); } System.out.println(); } |
|
返回顶楼 | |
发表时间:2009-12-31
最后修改:2009-12-31
通过设置变量 margin来表示与边界的距离,一圈一圈循环往里设置数值。
#!/usr/bin/env python def printEddeString(len): a = [[0 for i in range(len)] for j in range(len)] num = 0 count = 0 margin = 0 a[0][1] = 100 while True: for i in range(margin,len-margin): count += 1 a[margin][i] = count if count >= len**2: break for i in range(margin+1,len-margin): count += 1 a[i][len-margin-1] = count for i in range(len-margin-2,margin-1,-1): count += 1 a[len-margin-1][i] = count for i in range(len-margin-2,margin,-1): count += 1 a[i][margin] = count if count >= len**2: break margin += 1 for i in range(len): print a[i] if __name__ == '__main__': printEddeString(8) |
|
返回顶楼 | |
发表时间:2010-01-05
10几年前 刚刚学basic时做个这个题
|
|
返回顶楼 | |
发表时间:2010-03-04
感觉这里变成了一个Java的论坛了
|
|
返回顶楼 | |
发表时间:2010-04-05
螺旋数组!!!
|
|
返回顶楼 | |
发表时间:2010-04-05
public static void testCircle() { int size = 6; int count = size * size; int from = 1; int i = 0; int j = 0; //行方向 final boolean HORIZONTAL = true; //列方向 final boolean VERTICAL = false; //前进 final boolean GO_AHEAD = false; //后退 final boolean BACK_OFF = true; //目前方向 boolean direction = VERTICAL; boolean to = GO_AHEAD; //结果 int result[][] = new int[size][size]; //小框框索引,就是外围那个框 int endIndex = size - 1; int fromIndex = 0; while(from <= count) { result[i][j] = from; from++; //行方向前进 if(direction == HORIZONTAL && to == GO_AHEAD) { i++; } //行方向后退 if(direction == HORIZONTAL && to == BACK_OFF) { i--; } //列方向前进 if(direction == VERTICAL && to == GO_AHEAD) { j++; } //列方向后退 if(direction == VERTICAL && to == BACK_OFF) { j--; } //1.到达列末尾,转为行方向前进 if(i == fromIndex && j == endIndex) { //改变方向为行方向且是前进 direction = HORIZONTAL; to = GO_AHEAD; //从最后一列第一行开始前进 } //2.行方向到达末尾,转为列方向且为后退 if(i == endIndex && j == endIndex) { //改变方向为横向且是后退 direction = VERTICAL; to = BACK_OFF; //从最后一列最后一行开始后退 } //3.列方向到达开头,转为行方向且为后退 if(i == endIndex && j == fromIndex) { //改变方向为横方向且后退 direction = HORIZONTAL; to = BACK_OFF; //从第一列最后一行开始后退 } //4.行方向到达开头,转为列方向且为前进,新的一轮开始 if(i == fromIndex && j == fromIndex) { //改变方向为竖向且是后退 direction = VERTICAL; to = GO_AHEAD; //从新的第一行第一列开始前进 //新的一轮开始 fromIndex++; endIndex--; i = fromIndex; j = fromIndex; } } for(int m = 0; m < size; m++) { for(int k = 0; k < size; k++) System.out.print(String.format("%5d", result[m][k])); System.out.println(); } } 按照面向对象方式思考的代码,但不是对象,懒得整理成对象了,可以重构成类,然后把相应状态转换成方法就可以了 |
|
返回顶楼 | |
发表时间:2010-04-08
使用递归算法,打印循环数字 public class Test { private static int len = 7; private static int[][] snake = new int[len][len]; public static void main(String[] args) { printCyleNum(snake.length, 0); for (int x = 0; x < snake.length; x++) { for (int y = 0; y < snake[x].length; y++) { String str = (++snake[x][y]) + ""; if (snake[x][y] >= 10) { str = " " + str; } else { str = " " + str; } System.out.print(str); } System.out.println(); } } /** * 使用递归算法,打印循环数字 * @param step 步长 * @param startNum 初始值 */ public static void printCyleNum(int step, int startNum) { int length = snake.length; int x = (length - step)/2; int y = (length - step)/2; if (step == 0) { return; } if (step == 1) { snake[x][y] = startNum; } else { for (; y < step + (length - step)/2; y++) { snake[x][y] = startNum++; } y = y - 1; for (++x; x < step + (length - step)/2; x++) { snake[x][y] = startNum++; } x = x - 1; for (--y; y >= (length - step)/2; y--) { snake[x][y] = startNum++; } y = y + 1; for (--x; x > (length - step)/2; x--) { snake[x][y] = startNum++; } printCyleNum(step - 2, startNum); } } }
|
|
返回顶楼 | |
发表时间:2010-04-08
最后修改:2010-04-08
贴个groovy的
class Snake { def x=0; def y=0; def map; def bottom; def top; def left={ this.x-=1; if(this.x-1<this.top){ this.next=this.up; this.top+=1; } } def right={ this.x+=1 if(this.x+1>this.bottom){ this.next=this.down; } } def down={ this.y+=1 if(this.y+1>this.bottom){ this.next=this.left; } } def up={ this.y-=1 if(this.y-1<this.top){ this.next=this.right; this.bottom-=1; } } def next=right; def move(){ if (this.bottom==null){ this.top=0; this.bottom=(map.length-1); } for(def i in 1..(map.length*map.length)){ map[this.y][this.x]=i; this.next(); } } static main(args) { def size=5; def int[][] square=new int[size][size]; for(def i in 1..size){ for(def j in 1..size){ square[i-1][j-1]=0 } } new Snake(['map':square]).move(); println size; square.each(){ println it; } } } 4 [1, 2, 3, 4] [12, 13, 14, 5] [11, 16, 15, 6] [10, 9, 8, 7] 5 [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] |
|
返回顶楼 | |