求高手解一面试题!

hjx_gb2000 2010-06-19 04:31:06
编写程序实现如下的输出,一个N*N的矩阵(N可输入)
如:N=4
输出:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
...全文
381 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjx_gb2000 2010-06-23
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 thc1987 的回复:]
在ACM中,这样分析:

第1个circle 数量为4n-4,即 4(n-0)-4
第2个circle n'=n-2,数量4n'-4=4(n-2)-4=4n-12
第3个circle n''=n-4,数量4''-4=4(n-4)-4=4n-20
......
可得每circle的数量为4(n-(i-1)*2)-4,i为第几圈

由于最后1圈要么=4,要么=0(……
[/Quote]

经验啊
hncandy 2010-06-21
  • 打赏
  • 举报
回复
哇,很奇妙啊,跟高手学习了!
BigKing911 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 thc1987 的回复:]

在ACM中,这样分析:

第1个circle 数量为4n-4,即 4(n-0)-4
第2个circle n'=n-2,数量4n'-4=4(n-2)-4=4n-12
第3个circle n''=n-4,数量4''-4=4(n-4)-4=4n-20
......
可得每circle的数量为4(n-(i-1)*2)-4,i为第几圈

由于最后1圈要么=4,要么=……
[/Quote]
太有才了吧?
猿敲月下码 2010-06-21
  • 打赏
  • 举报
回复
玩一把 :)
package com.csdn;

public class TestRec {

public static final int MIN = 2;
public static final int MAX = 9;

enum direction {
up, down, left, right
}

public static void main(String[] args) {
int n = 4; // 这里可以自行输入
int number, number_max;
int x, y;
int boundary_top, boundary_bottom, boundary_left, boundary_right;
int[][] matrix = new int[MAX][MAX];
direction current_direction = direction.right;

if ((n < MIN) || (n > MAX))
return;

boundary_top = 0;
boundary_bottom = n - 1;
boundary_left = 0;
boundary_right = n - 1;
x = 0;
y = 0;
number_max = n * n;

for (number = 1; number <= number_max; number++) {
matrix[x][y] = number;

switch (current_direction) {
case right:
if (++x == boundary_right) {
current_direction = direction.down;
boundary_top++;
}
break;
case down:
if (++y == boundary_bottom) {
current_direction = direction.left;
boundary_right--;
}
break;
case left:
if (--x == boundary_left) {
current_direction = direction.up;
boundary_bottom--;
}
break;
case up:
if (--y == boundary_top) {
current_direction = direction.right;
boundary_left++;
}
break;
default:
break;
}
}

for (y = 0; y < n; y++) {
for (x = 0; x < n; x++) {
System.out.printf("%5d", matrix[x][y]);
}
System.out.printf("\n");
}

}

}

----------
结果:
    1    2    3    4
12 13 14 5
11 16 15 6
10 9 8 7
猿敲月下码 2010-06-21
  • 打赏
  • 举报
回复
在ACM中,这样分析:

第1个circle 数量为4n-4,即 4(n-0)-4
第2个circle n'=n-2,数量4n'-4=4(n-2)-4=4n-12
第3个circle n''=n-4,数量4''-4=4(n-4)-4=4n-20
......
可得每circle的数量为4(n-(i-1)*2)-4,i为第几圈

由于最后1圈要么=4,要么=0(n为奇数,这圈只有值n*n,不计),所以可知 i=INT(n/2)

这样,你就建立了所谓的模型

接下来,ok,就随便你怎么写程序了

node ACM都是先建立模型,然后~coding才会很快~
等你积累到一定经验的时候,才会直接上来就coding

正所谓 看尽天下A× 心中自然无码~
keeya0416 2010-06-21
  • 打赏
  • 举报
回复
高手都已经解答了
我这里给个往外计数的输出了

public class Test {

public static void main(String[] args) {
test(6);
}
public static void test(int num){
int start = - (num - 1) / 2;
int end = num / 2;
int max;
int maxNum;
for(int i = start; i <= end; i++){
for(int j = start; j <= end; j++){
max = Math.max(Math.abs(i), Math.abs(j));
maxNum = (2 * max + 1) * (2 * max + 1);
if(i == - max){
System.out.print(maxNum - max + j);
}else if(j == - max){
System.out.print(maxNum - 3*max - i);
}else if(i == max){
System.out.print(maxNum - 5*max - j);
}else{
System.out.print(maxNum - 7*max + i);
}
System.out.print("\t");
}
System.out.println();
System.out.println();
}
}
}
测试结果:
21 22 23 24 25 26

20 7 8 9 10 27

19 6 1 2 11 28

18 5 4 3 12 29

17 16 15 14 13 30

36 35 34 33 32 31

aotian16 2010-06-21
  • 打赏
  • 举报
回复
学习
jiao_223 2010-06-21
  • 打赏
  • 举报
回复
高手这么多啊,自己好菜啊
hjx_gb2000 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 s394500839 的回复:]
面试这样的题目,面试官是为了考验你的什么能力?
[/Quote]

笔试题,要手写代码,汗
closewbq 2010-06-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 natalya13 的回复:]
Java code


Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int[][] nn = new int[n][n];
int temp = n * 2;
int length = temp / 2;
in……
[/Quote]
UP
storm_1984 2010-06-21
  • 打赏
  • 举报
回复
学习了
codes23457789 2010-06-21
  • 打赏
  • 举报
回复
面试官考的不是你编写程序的能力,看的是你写程序的习惯和格式!
我是辉子 2010-06-21
  • 打赏
  • 举报
回复
面试这样的题目,面试官是为了考验你的什么能力?
我是辉子 2010-06-21
  • 打赏
  • 举报
回复
都是高手,学习了
hjx_gb2000 2010-06-20
  • 打赏
  • 举报
回复
呵呵,厉害厉害!!
tjb1216 2010-06-20
  • 打赏
  • 举报
回复
很牛叉,待我来仔细看看
翼德兄 2010-06-20
  • 打赏
  • 举报
回复
楼上很谦虚嘛,程序写的很干练。
java1109 2010-06-20
  • 打赏
  • 举报
回复
厉害,都是高手!!
lacus87 2010-06-20
  • 打赏
  • 举报
回复
以前写的。。。方向和你要求的稍有不同~~

public class RoundArray {

public static int[][] createRoundArray(int n) {
int[][] roundArray = new int[n][n];
int[] add = { 1, 0 };
int size = n*n;
int x = 0;
int y = 0;
for (int i = 1; i <= size; i++) {
roundArray[x][y] = i;
int row = x + add[0];
int column = y + add[1];
if (row >= 0 && row < n && column >= 0 && column < n
&& roundArray[row][column] == 0) {
x = row;
y = column;
} else {
if (add[0] == 1) {
add[0] = 0;
add[1] = 1;
y = y + 1;
} else if (add[1] == 1) {
add[0] = -1;
add[1] = 0;
x = x - 1;
} else if (add[0] == -1) {
add[0] = 0;
add[1] = -1;
y = y - 1;
} else if (add[1] == -1) {
add[0] = 1;
add[1] = 0;
x = x + 1;
}
}
}
return roundArray;
}

public static void main(String[] args) {
int[][] array = RoundArray.createRoundArray(12);
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.printf("%3d",array[i][j]);
System.out.print(" ");
}
System.out.println();
}
}

result:
1 44 43 42 41 40 39 38 37 36 35 34
2 45 80 79 78 77 76 75 74 73 72 33
3 46 81 108 107 106 105 104 103 102 71 32
4 47 82 109 128 127 126 125 124 101 70 31
5 48 83 110 129 140 139 138 123 100 69 30
6 49 84 111 130 141 144 137 122 99 68 29
7 50 85 112 131 142 143 136 121 98 67 28
8 51 86 113 132 133 134 135 120 97 66 27
9 52 87 114 115 116 117 118 119 96 65 26
10 53 88 89 90 91 92 93 94 95 64 25
11 54 55 56 57 58 59 60 61 62 63 24
12 13 14 15 16 17 18 19 20 21 22 23

高亮 2010-06-19
  • 打赏
  • 举报
回复
高手好多,我速度太慢了
加载更多回复(6)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧