public class huixingshu {
public static void main(String [] args) {
Scanner in = new Scanner(System.in) ;
System.out.println("请输入数字");
int len = in.nextInt() ;
int[][] arr = new int[len][len] ;
int s = len * len ;
// k=1:向右 k=2:向下 k=3:向左 k=4:向上
int k = 1 ;
int i = 0 , j = 0 ;
for(int m = 1 ; m <= s ; m++) {
if(k == 1) {
if (j < len && arr[i][j] == 0) {
arr[i][j++] = m ;
} else {
k = 2 ;
i++ ;
j-- ;
m-- ;
}
}else if(k == 2) {
if (i < len && arr[i][j] == 0) {
arr[i++][j] = m ;
} else {
k = 3 ;
i-- ;
j-- ;
m--;
}
} else if(k == 3) {
if (j >= 0 && arr[i][j] == 0) {
arr[i][j--] = m ;
}else {
k = 4 ;
i-- ;
j++ ;
m-- ;
}
} else if(k == 4) {
if(i >= 0 && arr[i][j] == 0) {
arr[i--][j] = m ;
} else {
k = 1 ;
i++ ;
j++ ;
m-- ;
}
}
}
for(int m = 0 ; m < arr.length ; m++) {
for(int n = 0 ; n < arr[m].length ; n++) {
System.out.print(arr[m][n] + "\t" );
}
System.out.println();
}
}
}
笔试题回形数(Java入门)
最新推荐文章于 2022-03-15 15:58:26 发布