题解 | #回型矩阵#
回型矩阵
http://www.nowcoder.com/practice/36d5dfddc22c4f5b88a5b2a9de7db343
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[][] array=new int[n][n]; int p=0,q=n-1,count=1; while (count<=n*n) { for (int i=p;i<=q;i++) array[p][i]=count++; for (int i=p+1;i<=q;i++) array[i][q]=count++; for (int i=q-1;i>=p;i--) array[q][i]=count++; for (int i=q-1;i>=p+1;i--) array[i][p]=count++; p++; q--; } for(int[] a:array) { for(int i:a) System.out.print(i+ " "); System.out.println(); } } }