锁定老帖子 主题:深圳一家公司面试问题,很囧
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-12
俺贴上一个不精炼的,
可以8是正方形,格式化输出 package 打印; public class 漩涡型数组 { private int[][] intArray_volution; private int intR = 0; //行 private int intC = 0; //列 private int intLength = 0; //总元素个数这个数字转成字符串后的长度 public 漩涡型数组(int intR, int intC) throws Exception{ super(); if(intR<0 ||intC <0){ throw new Exception("漩涡型数组构造函数传入的参数中有负数!"); } this.intR = intR; this.intC = intC; this.intArray_volution = new int[intR][intC]; this.intLength = (""+intR*intC).length(); } public static void main(String[] args) throws Exception{ 漩涡型数组 volutionObject = new 漩涡型数组(9,3); //初始化 volutionObject.fillData(); //填充数据 volutionObject.printData(); //打印数据 } /** * 填充数据 */ private void fillData(){ int intCurR = 0; int intCurC = -1; int i=1; boolean blnNoWay =true; //是否无路可逃 do{ blnNoWay =true; while(intCurC+1 <intC && intArray_volution[intCurR][intCurC+1]==0){ intArray_volution[intCurR][++intCurC] = i++; blnNoWay = false; } while(intCurR+1 <intR && intArray_volution[intCurR+1][intCurC]==0){ intArray_volution[++intCurR][intCurC] = i++; blnNoWay = false; } while(intCurC-1 >-1 && intArray_volution[intCurR][intCurC-1]==0){ intArray_volution[intCurR][--intCurC] = i++; blnNoWay = false; } while(intCurR-1 >-1 && intArray_volution[intCurR-1][intCurC]==0){ intArray_volution[--intCurR][intCurC] = i++; blnNoWay = false; } }while(!blnNoWay); //一轮下来,四个方向都没有移动过,则是无路可走 } /** * 打印数据 */ private void printData(){ StringBuilder sb= new StringBuilder(); for (int i = 0; i < intR; i++) { for (int j = 0; j < intC; j++) { formatInt2String(sb,intArray_volution[i][j]); } sb.deleteCharAt(sb.length()-1).append("\n"); } System.out.println(sb); } /** * 格式化要打印的int */ private StringBuilder formatInt2String(StringBuilder sb, int intValue){ for (int i = (intValue+"").length(); i < intLength; i++) { sb.append(" "); } sb.append(intValue).append(","); return sb; } } |
|
返回顶楼 | |
发表时间:2009-12-12
好强啊 膜拜中。也学习了
|
|
返回顶楼 | |
发表时间:2009-12-12
前面的好像都很复杂,给个我自己做的简洁做法,我正考虑以后拿来做面试题,不过今天让我的两个开发做了一下,水平还行,但是用了一早上时间,所以适不适合作面试题还有待斟酌
public static void print(int count) { int is[][] = new int[count][count]; int i = 0; int c = count * count; // 横向坐标累加器 int j = 0; // 纵向坐标累加器 int k = 0; // 横纵向控制,1为横向,-1为纵向 int m = 1; // 坐标累加器,1为递增,-1为递减 int n = 1; while (i < c) { is[j][k] = ++i; if (m > 0) { k += n; // 触边转向 if (k < 0 || k >= count || is[j][k] != 0) { m *= -1; k -= n; j += n; } } else { j += n; // 触边转向 if (j < 0 || j >= count || is[j][k] != 0) { m *= -1; j -= n; n *= -1; k += n; } } } for (int p = 0; p < count; ++p) { for (int q = 0; q < count; ++q) System.out.print(is[p][q] + "\t"); System.out.println(); } } |
|
返回顶楼 | |
发表时间:2009-12-12
最后修改:2009-12-12
看一下我的,思路应该更清晰一点: public class SnakePrint { private int orient = 0, length = 0, x = 0, y = 0; // orients为顺时针90°旋转方向,前进步长为1 private int[][] orients = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; private int[][] arrays; public SnakePrint(int length) { this.length = length; arrays = new int[length][length]; } // 根据当前方向返回下一个前进方向 private int[] nextOrient(int[] curOrient) { int nextX = x + curOrient[0], nextY = y + curOrient[1]; // 前进方向需要顺时针旋转90°了 if (nextX < 0 || nextX >= length || nextY < 0 || nextY >= length || arrays[nextX][nextY] != 0) { orient = ++orient % 4; return orients[orient]; } return curOrient; // 不需要掉头,返回原前进方向 } public void print() { int[] curOrient = orients[orient]; // 初始前进方向 for (int i = 1; i <= length * length; i++) { // 依次填充数组 arrays[x][y] = i; curOrient = nextOrient(curOrient); x += curOrient[0]; y += curOrient[1]; } for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { System.out.printf("%4d", arrays[i][j]); // 按固定4个字符宽度的格式输出 } System.out.println(); } } public static void main(String[] args) { SnakePrint snakePrint = new SnakePrint(6); snakePrint.print(); } } |
|
返回顶楼 | |
发表时间:2009-12-13
最后修改:2009-12-13
比较有趣的问题,用来练习 clojure 语言。代码如下:
(defn number-pos [num side] (if (= num 1) [1 1] (let [[p-x p-y] (number-pos (dec num) side)] (cond (and (= p-y side) (< p-x side)) [(inc p-x) p-y] (and (= p-x side) (> p-y 1)) [p-x (dec p-y)] (= p-x 1) [p-x (inc p-y)] (= p-y 1) [(dec p-x) p-y])))) (defn boxing [side] (if (= side 1) [[1 1]] (let [nums (range 1 (- (* 4 side) 3))] (map #(number-pos % side) nums)))) (defn boxes [side] (map boxing (range side 0 -2))) (defn trans-box [box layer] (for [[x y] box] [(+ x layer) (+ y layer)])) (defn put-boxes [boxes] (reduce concat (map #(trans-box %1 %2) boxes (iterate inc 0)))) (defn make-it [side] (let [orders (put-boxes (boxes side)) pos-map (map #(hash-map :pos %1 :num %2) orders (iterate inc 1)) sorting (fn [{[x y] :pos}] (+ (* x 1000) y)) pos-map (sort-by sorting pos-map)] (map :num pos-map))) (defn print-it [side] (doseq [row (partition side (make-it side))] (let [s (apply str (interpose " " row))] (println s)))) (print-it 5) (print-it 6) |
|
返回顶楼 | |
发表时间:2009-12-13
有点像巡路算法,往前走,走不通就右拐
|
|
返回顶楼 | |
发表时间:2009-12-13
最后修改:2009-12-13
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");
|
|
返回顶楼 | |
发表时间:2009-12-13
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");
|
|
返回顶楼 | |
发表时间:2009-12-14
转为二维数组,挺历害的
|
|
返回顶楼 | |
发表时间:2009-12-14
最后修改:2009-12-14
数组行列互换 + 递归
static void populateArray(int minValue, int rowNum, int colNum, int[][] toBePopulatedArray) { for (int colIdx = 0; colIdx < colNum; colIdx++) { toBePopulatedArray[0][colIdx] = minValue++; } //Recursive population if (rowNum > 1 || colNum > 1) { int newRowNum = colNum; int newcolNum = rowNum - 1; int[][] subArray = new int[newRowNum][newcolNum]; populateArray(minValue, newRowNum, newcolNum, subArray); for (int colIdx = colNum - 1; colIdx > -1; colIdx--) { for (int rowIdx = 1; rowIdx < rowNum; rowIdx++) { toBePopulatedArray[rowIdx][colIdx] = subArray[newRowNum - colIdx - 1][rowIdx - 1]; } } } } |
|
返回顶楼 | |