论坛首页 招聘求职论坛

深圳一家公司面试问题,很囧

浏览 78196 次
精华帖 (0) :: 良好帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-10  
一个画图程序 要求打印出

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

   发表时间:2009-12-10  
一般都是转圈圈打印的 有没有数学帝能找到规律 一行一行的打印出来
0 请登录后投票
   发表时间:2009-12-10  
一般限时多少做出来呢?
0 请登录后投票
   发表时间:2009-12-10   最后修改:2009-12-10
这个恶心的题貌似原来学c语言见过, 我来贴一个别人解决方案,转化为二维数组初始化问题。
0 请登录后投票
   发表时间:2009-12-10   最后修改:2009-12-12
贴一个可以跑的
class snakePrint {
	static int length = 7;
	static int value = 1;
	static int[][] snake = new int[length][length];
	static Direction lastDirection = Direction.Right;

	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();
		}
	}
}
3 请登录后投票
   发表时间:2009-12-10  
撒腿跑人。
0 请登录后投票
   发表时间:2009-12-10  
貌似是小学奥数算法?
忘记了, 待我有空想想。
0 请登录后投票
   发表时间:2009-12-10  
好像通过多重取模可以算出来任何数值的位置。
0 请登录后投票
   发表时间:2009-12-10  
public static void main(String args[]){
	  int N=5;
	  int a[][]=new int[N][N];
	  int i=0,j=0;
	  int count=1;
	  for(i=0;i<N;i++){
		  for(j=0;j<N;j++){
			  a[i][j]=0;
		  }
	  }
	  i=0;
	  j=0;
      for(int k=0;k<=N/2;k++){
    	  i=k;
    	  j=k;
		  for(i=k;i<N-k;i++){
			  a[j][i]=count;
			  count++;
		  }
		  i=N-k-1;
		  for(j=k+1;j<N-k;j++){
			  a[j][i]=count;
			  count++;
		  }
		  j=N-k-1;
		  for(i=N-k-2;i>=k;i--){
			  a[j][i]=count;
			  count++;
		  }
		  i=k;
		  for(j=N-k-2;j>=1+k;j--){
			  a[j][i]=count;
			  count++;
		  }
      }
		 
	  for(i=0;i<N;i++){
		  for(j=0;j<N;j++){
		     System.out.print(a[i][j]+" ");
		  }
		  System.out.println();
	  }
}

 附言:这个是一个ACM训练题  

0 请登录后投票
   发表时间:2009-12-10  
caiwenhn2008 写道
贴一个可以跑的
class snakePrint {
	static int length = 7;
	static int value = 1;
	static int[][] snake = new int[length][length];
	static Direction lastDirection = Direction.Right;

	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();
		}
	}
}

算法好的真厉害 虽然我喜欢这样的题 但我做的还真不行 白干了几年
4 请登录后投票
论坛首页 招聘求职版

跳转论坛:
Global site tag (gtag.js) - Google Analytics