public class CircleNumber {
private int[][] Matrix;
private int Size;
public CircleNumber(int size){
this.Size=size;
InitMatrix();
}
private void InitMatrix(){
Matrix = new int[Size][];
for(int i=0;i<Size;i++){
Matrix[i]=new int[Size];
}
}
public void FormCircleNumber(){
int Num=this.Size*this.Size;
int Row=Size;
int Col=Size;
int i=0,j=0,k=0;
int tmp=1;
int LeftFlag=0;
int UpFlag=0;
while((Num>tmp)||(Num==tmp)){ //顺时针旋转
if((j+1<Col)&&(LeftFlag==0)&&(UpFlag==0)){ //向右
Matrix[i][j]=tmp;
j++;
}else if(j+1==Col&&i+1<Row){//向下
Matrix[i][j]=tmp;
i++;
if(i==Row-1){
LeftFlag=1;
}
}else if((i+1==Row)&&(LeftFlag==1)&&(j-1>=k)){//向左
Matrix[i][j]=tmp;
j--;
if(j==k){
UpFlag=1;
LeftFlag=0;
}
}else if((j==k)&&(UpFlag==1)&&(i-1)>=k){//向上
Matrix[i][j]=tmp;
i--;
}else{ //完成一圈为下一圈准备数据
k++;
i=k;
j++;
Col--;
Row--;
LeftFlag=0;
UpFlag=0;
tmp--;
}
tmp++;
}
}
public void Display(){
int i,j;
for(i=0;i<this.Size;i++){
for(j=0;j<this.Size;j++){
System.out.print(Matrix[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CircleNumber cirnber=new CircleNumber(16) ;
cirnber.FormCircleNumber();
cirnber.Display();
}
}