fork download
  1. public class Main
  2. {
  3. public static void main(String[] args)
  4. {
  5. int w = 5, h = 7;
  6. int[][] arr = new int[w][h];
  7. int count = 1;
  8. for (int i = 0; count <= w*h; i++)
  9. {
  10. for (int x = i; x < w-i && count <= w*h; x++)
  11. arr[x][i] = count++;
  12. for (int y = i+1; y < h-i && count <= w*h; y++)
  13. arr[w-i-1][y] = count++;
  14. for (int x = w-2-i; x >= i && count <= w*h; x--)
  15. arr[x][h-i-1] = count++;
  16. for (int y = h-2-i; y > i && count <= w*h; y--)
  17. arr[i][y] = count++;
  18. }
  19. for (int y = 0; y < h; y++)
  20. {
  21. for (int x = 0; x < w; x++)
  22. System.out.printf("%3d",arr[x][y]);
  23. System.out.println();
  24. }
  25. }
  26. }
  27.  
Success #stdin #stdout 0.09s 380160KB
stdin
Standard input is empty
stdout
  1  2  3  4  5
 20 21 22 23  6
 19 32 33 24  7
 18 31 34 25  8
 17 30 35 26  9
 16 29 28 27 10
 15 14 13 12 11