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) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. //Spiral Matrix
  14. Scanner in=new Scanner(System.in);
  15. int n=in.nextInt();
  16. int a[][]=new int[n][n];
  17. for(int i=0;i<n;i++)
  18. {
  19. for(int j=0;j<n;j++)
  20. {
  21. a[i][j]=in.nextInt();
  22. }
  23. }
  24. for(int i=0;i<n;i++)
  25. {
  26. if(i%2==0)
  27. {
  28. for(int j=0;j<n;j++)
  29. {
  30. System.out.print(a[i][j]+" ");
  31. }
  32. System.out.println();
  33. }
  34. else if(i%2!=0)
  35. {
  36. for(int j=n-1;j>=0;j--)
  37. {
  38. System.out.print(a[i][j]+" ");
  39. }
  40. System.out.println();
  41. }
  42. }
  43. }
  44. }
Success #stdin #stdout 0.13s 36056KB
stdin
3
1 2 3 4 5 6 7 8 9
stdout
1 2 3 
6 5 4 
7 8 9