fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args)
  11. {
  12. int n = 10;
  13.  
  14. int[][] values = new int[n][n];
  15.  
  16. for (int i = 0; i < n * n; ++i)
  17. {
  18. double r = Math.floor(Math.sqrt(i));
  19. int col = (int) ((Math.round(r / 2) * Math.pow(-1, r + 1))
  20. + (Math.pow(-1, r + 1) * (((r * (r + 1)) - i) - Math.abs((r * (r + 1)) - i)) / 2));
  21.  
  22. int row = (int) ((Math.round(r / 2) * Math.pow(-1, r))
  23. + (Math.pow(-1, r + 1) * (((r * (r + 1)) - i) + Math.abs((r * (r + 1)) - i)) / 2));
  24.  
  25. values[row + (n / 2)][col + ((n - 1) / 2)] = (n * n) - i;
  26. }
  27.  
  28. if (n % 2 == 0)
  29. {
  30. for (int row = 0; row < n; ++row)
  31. {
  32. for (int col = 0; col < n; ++col)
  33. {
  34. System.out.print(values[row][col] + "\t");
  35. }
  36. System.out.println();
  37. }
  38. }
  39. else
  40. {
  41. for (int row = n - 1; row >= 0; --row)
  42. {
  43. for (int col = n - 1; col >= 0; --col)
  44. {
  45. System.out.print(values[row][col] + "\t");
  46. }
  47. System.out.println();
  48. }
  49. }
  50.  
  51. }
  52. }
Success #stdin #stdout 0.1s 27960KB
stdin
Standard input is empty
stdout
1	2	3	4	5	6	7	8	9	10	
36	37	38	39	40	41	42	43	44	11	
35	64	65	66	67	68	69	70	45	12	
34	63	84	85	86	87	88	71	46	13	
33	62	83	96	97	98	89	72	47	14	
32	61	82	95	100	99	90	73	48	15	
31	60	81	94	93	92	91	74	49	16	
30	59	80	79	78	77	76	75	50	17	
29	58	57	56	55	54	53	52	51	18	
28	27	26	25	24	23	22	21	20	19