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. printSpiral1(new int[][]{{1,2}}, 1 ,2);
  13. printSpiral1(new int[][] {{ 1, 2, 3 }, { 16, 17, 4 }, { 15, 18, 5 },{ 14, 19, 6 }, { 13, 20, 7 },{ 12, 21, 8 },{ 11, 10, 9 } }, 7 ,3);
  14.  
  15. }
  16.  
  17. public static void printSpiral1(int array[][],int row,int col){
  18.  
  19. int rowStart=0,colStart=0,rowEnd=row-1,colEnd=col-1;
  20. int i;
  21.  
  22. while(rowStart<=rowEnd && colStart<= colEnd){
  23.  
  24. for(i=colStart;i<=colEnd;i++)
  25. System.out.print(" "+array[rowStart][i]);
  26.  
  27. for(i=rowStart+1;i<=rowEnd;i++)
  28. System.out.print(" "+array[i][colEnd]);
  29.  
  30. for(i=colEnd-1;i>=colStart;i--)
  31. System.out.print(" "+array[rowEnd][i]);
  32.  
  33. for(i=rowEnd-1;i>=rowStart+1;i--)
  34. System.out.print(" "+array[i][colStart]);
  35.  
  36. rowStart++;
  37. colStart++;
  38. rowEnd--;
  39. colEnd--;
  40. }
  41. System.out.println();
  42. }
  43. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
 1 2 1
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 20 19 18