Problem Description

Input

Output

Sample Input

4

Sample Output

1  2  3  4 12        5 11        6     10  9  8  7

import java.util.Scanner;public class Main{  public static void main(String[] args) {    Scanner sc = new Scanner(System.in);    while(sc.hasNext()){      int n =sc.nextInt();      if(n<=0){        break;      }      if(n==1){        System.out.println("  1");        continue;      }      //上面的数字      int up[] = new int[n];      //下面的数字      int down[] = new int[n];      //右边的数字      int right[] = new int[n-2];      //左边的数字      int left[] = new int[n-2];      int t=1;      for(int i=0;i<n;i++){        up[i]=t;        t++;      }      for(int i=0;i<n-2;i++){        right[i]=t;        t++;      }      for(int i=0;i<n;i++){        down[i]=t;        t++;      }      for(int i=0;i<n-2;i++){        left[i]=t;        t++;      }      int r=0;      int f=n-3;      for(int i=0;i<n;i++){                for(int j=0;j<n;j++){          if(i==0){            System.out.printf("%3d",up[j]);          }else if(i==n-1){            System.out.printf("%3d",down[n-1-j]);          }else{            if(j==0){              System.out.printf("%3d",left[f]);              f--;            }else if(j==n-1){              System.out.printf("%3d",right[r]);              r++;            }else{              System.out.print("   ");            }          }        }        System.out.println();      }    }  }}