一个算法题! 描述如下:n=1时:输出 0 1 0 4 5 2 0 3 0n=2时:输出: 0 1 2 0 8 9 10 3 7 12 11 4 0 6 5 0 依次类推,螺旋输出。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class HelixTest{ private static int[][] helixNum(int n) { int len=n+2; int[][] array=new int[len][len]; int count = len*len-4; int direct = 0; int round = 1; for(int index = 1, x = 0, y = 0; index <= count; index++) { if(((x==0)&&(y==0||y==len-1))||(x==len-1)&&(y==0||y==len-1)){ array[x][y]=0; index--; } else{ array[x][y] = index; } switch(direct) { case 0: if(y < len - round) y++; else { direct = 1; x++; } break; case 1: if(x < len - round) x++; else { direct = 2; y--; } break; case 2: if(y >= round) y--; else { direct = 3; x--; } break; case 3: if(x > round) x--; else { direct = 0; round++; y++; } break; } } return array; } public static void main(String[] args){ int[][] intArr=helixNum(2); for(int i=0;i<intArr.length;i++){ for(int j=0;j<intArr[i].length;j++){ System.out.print(intArr[i][j]+" "); } System.out.println(); } }} 中文的length问题 模是什么意思? 如何执行abc.jar中的类A和类B? java中什么是成员变量,什么是本地便量? 怎么搞的,出现以下问题。 jsp引入javabean的时候出现的问题,急用 求有关applet的电子书籍 今天加班的兄弟们辛苦了,散分 一个最简单的问题,送分! java网络读取遇到-1的疑惑 焦点在控件之间的移动 一个关于elipse 的问题
public class HelixTest
{
private static int[][] helixNum(int n) {
int len=n+2;
int[][] array=new int[len][len];
int count = len*len-4;
int direct = 0;
int round = 1;
for(int index = 1, x = 0, y = 0; index <= count; index++) {
if(((x==0)&&(y==0||y==len-1))||(x==len-1)&&(y==0||y==len-1)){
array[x][y]=0;
index--;
}
else{
array[x][y] = index;
}
switch(direct) {
case 0:
if(y < len - round)
y++;
else {
direct = 1;
x++;
}
break;
case 1:
if(x < len - round)
x++;
else {
direct = 2;
y--;
}
break;
case 2:
if(y >= round)
y--;
else {
direct = 3;
x--;
}
break;
case 3:
if(x > round)
x--;
else {
direct = 0;
round++;
y++;
}
break;
}
}
return array;
}
public static void main(String[] args){
int[][] intArr=helixNum(2);
for(int i=0;i<intArr.length;i++){
for(int j=0;j<intArr[i].length;j++){
System.out.print(intArr[i][j]+" ");
}
System.out.println();
} }
}