今天在javaEye论坛上看到有人说看到面试题让写出回旋数,后面好多人用不同的语言,不同的算法给出了回旋数的实现。感觉很有意思,自己也挖空心思写了个。先秀下自己的,看大家有没有什么好算法,特别是javaEye有人用递归实现了,但是不适用java语言,本人水平有限看不懂。看看咱们论坛是怎么用递归实现的。呵呵所谓回旋数
int n = 3
1 2 3
8 9 4
7 6 5int n = 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 没写注释,变量命名也很随意,可能比较难懂,但是运行没问题public class suanfa {
//回旋矩阵
public static void main(String[] args){
suanfa sf = new suanfa();
sf.huixuan(5);
}
public void huixuan(int n){
int[][] out = new int[n][n];
int i = 0;
int j = 0;
int con = 0;
int lincon = n ;
int rowcon = n ;
int m = 1;
int casenum = 1;
while (m < n * n + 1) {
out[i][j] = m;
con ++ ;
m ++;
switch(casenum){
case 1:
if(con == lincon) {
casenum = 2;
lincon --;
i++;
con = 1;
}else {
j++;
}
break;
case 2:
if(con == rowcon) {
casenum = 3;
rowcon -- ;
j--;
con = 0;
}else i++;
break;
case 3:
if(con == lincon) {
casenum = 4;
lincon -- ;
i--;
con = 1;
}else j--;
break;
case 4:
if(con == rowcon) {
casenum = 1;
rowcon -- ;
j++;
con = 0;
}else i--;
break;
}
} for (i =0 ;i < n;i++){
for(j=0;j<n;j++){
System.out.print(out[i][j]+ " ");
}
System.out.println();
}
}
}
int n = 3
1 2 3
8 9 4
7 6 5int n = 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 没写注释,变量命名也很随意,可能比较难懂,但是运行没问题public class suanfa {
//回旋矩阵
public static void main(String[] args){
suanfa sf = new suanfa();
sf.huixuan(5);
}
public void huixuan(int n){
int[][] out = new int[n][n];
int i = 0;
int j = 0;
int con = 0;
int lincon = n ;
int rowcon = n ;
int m = 1;
int casenum = 1;
while (m < n * n + 1) {
out[i][j] = m;
con ++ ;
m ++;
switch(casenum){
case 1:
if(con == lincon) {
casenum = 2;
lincon --;
i++;
con = 1;
}else {
j++;
}
break;
case 2:
if(con == rowcon) {
casenum = 3;
rowcon -- ;
j--;
con = 0;
}else i++;
break;
case 3:
if(con == lincon) {
casenum = 4;
lincon -- ;
i--;
con = 1;
}else j--;
break;
case 4:
if(con == rowcon) {
casenum = 1;
rowcon -- ;
j++;
con = 0;
}else i--;
break;
}
} for (i =0 ;i < n;i++){
for(j=0;j<n;j++){
System.out.print(out[i][j]+ " ");
}
System.out.println();
}
}
}
public static void main(String[] args) {
int[][] a;
HuiXuanShu hxs = new HuiXuanShu();
a = hxs.huixuan(4);
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
} public int[][] huixuan(int n) {
if (n == 1) {
return new int[][]{{1}};
}
if(n == 2){
return new int[][]{{1,2},{4,3}};
}
int[][] result = new int[n][n];
for (int i = 0; i < n; i++) {
result[0][i] = i + 1;
}
for (int i = 0; i < n; i++) {
result[i][n - 1] = n + i;
}
for (int i = n - 1; i >= 0; i--) {
result[n - 1][i] = 3 * n - 2 - i;
}
for (int i = n - 1; i > 0; i--) {
result[i][0] = 4 * n - 3 - i;
}
int aaa = 4 * n - 4; int[][] result1 = huixuan(n - 2);
for (int i = 0; i < n - 2; i++) {
for (int j = 0; j < n - 2; j++) {
result[i + 1][j + 1] = result1[i][j] + aaa;
}
}
return result;
}
}
public class Test {
Integer[][] a = null; int len = 0; int start = 1; /**
* @param args
*/
public static void main(String[] args) { Test t = new Test();
t.test(7);
t.print(7);
} public void print(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] != null && a[i][j] < 10) {
System.out.print(" " + a[i][j]);
} else {
System.out.print(" " + a[i][j]);
}
}
System.out.println();
}
} public void test(int n) { if (a == null) {
a = new Integer[n][n];
} List<Integer> arrs = new ArrayList<Integer>();
int length = (n - 1) * 4;
for (int i = 0; i < length; i++) {
arrs.add(start + i);
}
if (n == 1) {
int t = (int) Math.sqrt(start);
t = (t - 1) / 2;
a[t][t] = start;
}
for (int i = 0; i < n - 1; i++) {
a[len][i + len] = arrs.get(i);
if (i == 0) {
a[len][n - 1 + len] = arrs.get(i + n - 1);
a[n - 1 + len][n - 1 + len] = arrs.get(i + (n - 1) * 2);
a[n - 1 + len][len] = arrs.get(i + (n - 1) * 3);
} else {
a[i + len][n - 1 + len] = arrs.get(i + n - 1);
a[n - 1 + len][n - 1 - i + len] = arrs.get(i + (n - 1) * 2);
a[n - 1 - i + len][len] = arrs.get(i + (n - 1) * 3);
}
} if (n - 2 > 0) {
start = arrs.get(arrs.size() - 1) + 1;
len++;
test(n - 2);
} }
}
我写了个递归的,大家看看!