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. generateMatrixFile(4, 3);
  13. }
  14.  
  15. public static void generateMatrixFile(int n , int m) {
  16. int A[][] = new int[m][n]; // Switched m and n
  17. int k=1, c1=0, c2=n-1, r1=0, r2=m-1; // Switched m and n
  18. while (k<=n*m) {
  19. for (int i=c1;i<=c2 && k<=n*m;i++) {
  20. A[r1][i]=k++;
  21. }
  22. for(int j=r1+1;j<=r2 && k<=n*m;j++) {
  23. A[j][c2]=k++;
  24. }
  25. for(int i=c2-1;i>=c1 && k<=n*m;i--) {
  26. A[r2][i]=k++;
  27. }
  28. for(int j=r2-1;j>=r1+1 && k<=n*m;j--) {
  29. A[j][c1]=k++;
  30. }
  31. c1++;
  32. c2--;
  33. r1++;
  34. r2--;
  35. }
  36. System.out.println("The Matrix is:");
  37. for (int i=n-1;i>=0;i--) {
  38. for(int j=0 ; j < m ; j++) {
  39. System.out.print(A[j][i]+ "\t");
  40. }
  41. System.out.println();
  42. }
  43. }
  44. }
Success #stdin #stdout 0.09s 320576KB
stdin
Standard input is empty
stdout
The Matrix is:
4	5	6	
3	12	7	
2	11	8	
1	10	9