在javaEye上看到一道面试题,挺有趣,自己做了一下(题目:
http://www.javaeye.com/topic/545378)
要求打印出:
- int i=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
- int i=6
- 1 2 3 4 5 6
- 20 21 22 23 24 7
- 19 32 33 34 25 8
- 18 31 36 35 26 9
- 17 30 29 28 27 10
- 16 15 14 13 12 11
下面是自己花2个多小时做的:
- /**
- * 在屏幕打印出
- * -----------------
- * i = 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
- * -----------------
- * i=6
- * 1 2 3 4 5 6
- * 20 21 22 23 24 7
- * 19 32 33 34 25 8
- * 18 31 36 35 26 9
- * 17 30 29 28 27 10
- * 16 15 14 13 12 11
- * -----------------
- * i=7、8、···
- * 以此类推
- *
- * @author wsq198753@qq.com
- * 2010-12-2
- */
- public class CircleNumer {
- private static int N_TIME = 0;
- public CircleNumer(int n){
- this.N_TIME = n;
- }
- /**
- * 存数组
- * @param currentVal 当前值
- * @param array 存放要打印的数字的数组
- * @param circleNum 第几圈,拐弯后变
- * @param desc 是否逆序
- * @param isLine true:行,false:列
- */
- private void setArray(int currentVal,int[][] array,int circleNum,boolean desc,boolean isLine){
- //判断是否结束
- if(currentVal > N_TIME * N_TIME)
- return;
- //顺序
- if(!desc){
- //从第count/2 - count/4个开始,到N_TIME - count/4结束,++
- int begin = circleNum/2 - circleNum/4;
- int end = N_TIME - circleNum/4;
- //行
- if(isLine){
- //第几行
- int line = (circleNum-1)/4;
- for(int i = begin;i < end;i++)
- array[line][i] = currentVal++;
- }else{
- //第几列
- int row = N_TIME - (circleNum-1)/4 -1;
- for(int i = begin;i < end;i++)
- array[i][row] = currentVal++;
- }
- }//逆序
- else{
- //从第N_TIME - (count/2 - count/4) - 1个开始,到count/4结束,--
- int begin = N_TIME - circleNum/2 + circleNum/4 - 1;
- int end = circleNum/4;
- if(isLine){
- //第几行
- int line = N_TIME - (circleNum-1)/4 -1;
- for(int i = begin;i >= end;i--)
- array[line][i] = currentVal++;
- }else{
- //第几列
- int row = (circleNum-1)/4;
- for(int i = begin;i >= end;i--)
- array[i][row] = currentVal++;
- }
- }
- //count%2 == 0 改变desc
- if(circleNum%2 == 0)
- desc = !desc;
- circleNum++;
- setArray(currentVal,array,circleNum,desc,!isLine);
- }
- //打印屏幕
- public void printCircle(){
- int[][] array = new int[N_TIME][N_TIME];
- setArray(1,array,1,false,true);
- //加空格,为了美观
- int count = (int)Math.log10(N_TIME * N_TIME) + 1;
- for(int i=0;i < N_TIME;i++)
- for(int j=0;j < N_TIME;j++){
- //加空格,为了美观
- int numCount = (int)Math.log10(array[i][j]) + 1;
- System.out.print(array[i][j] + " ");
- for(int k=0;k < (count - numCount);k++)
- System.out.print(" ");
- if(j == (N_TIME-1)) System.out.println();
- }
- }
- public static void main(String[] args) {
- CircleNumer cn = new CircleNumer(6);
- cn.printCircle();
- }
- }
学习别人怎么做的:
1、例子1
- 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();
- }
- }
2、例子2:
- class SnakePrint {
- static int length = 7;
- static int value = 1;
- static int[][] snake = new int[length][length];
- static Direction lastDirection = Direction.Right;
- //枚举需要jdk1.5
- 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();
- }
- }
- }