public class Rotating {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rotating x=new Rotating(6);
x.output();
}
int width=4;//只能是偶数
Integer[][] Zone;
Rotating(int p){
this.width=p%2==1?p-1:p;
this.Zone=new Integer[this.width][this.width];
this.init(0, 1);
}
private void init(int roll,int start){
int wid=this.width-2*roll;
System.out.println("kaishi start="+start+" wid="+wid);
if(wid==0)return;
for(int i=0;i<wid-1;i++){
this.Zone[roll][i+roll]=start;
System.out.println("start="+start);
start++;
}
for(int i=0;i<wid-1;i++){
this.Zone[i+roll][this.width-1-roll]=start;
System.out.println("start="+start);
start++;
}
for(int i=0;i<wid-1;i++){
this.Zone[this.width-1-roll][this.width-1-roll-i]=start;
System.out.println("start="+start);
start++;
}
for(int i=0;i<wid-1;i++){
this.Zone[this.width-1-roll-i][roll]=start;
System.out.println("start="+start);
start++;
}
init(++roll,start);
}
void output(){
for(int i=0;i<this.width;i++){
for(int j=0;j<this.width;j++){
System.out.print(" "+this.Zone[i][j]);
}
System.out.println();
}
}
}
这是帮你找的代码,你可以看一下,不过结果就是如你写的那样 |