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 rotation90Droite(int [][] vals) {
  11.  
  12. int e = vals.length-1;
  13. int c = e / 2;
  14. for (int i = 0; i <= c ; i++) {
  15. for (int j = i; j <e - i; j++) {
  16. int t = vals[i][j];
  17.  
  18.  
  19. vals[i][j] = vals[e - j][i];
  20.  
  21. vals[e - j][i] = vals[e - i][e - j];
  22.  
  23. vals[e - i][e - j] = vals[j][e - i];
  24.  
  25. // This line is corrected: "e - j" -> "e - i"
  26. vals[j][e - i] = t;
  27. }
  28. }
  29. }
  30. public static void printMatrix(int[][] Matrix) {
  31.  
  32. for(int i = 0; i < Matrix.length; i++) {
  33. for (int j = 0; j < Matrix.length; j++) {
  34. System.out.print(Matrix[i][j]+ " ");
  35. }
  36. System.out.println(" ");
  37. }
  38. }
  39.  
  40.  
  41. public static void main(String[] args) {
  42.  
  43. int[][] vals = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}} ;
  44. printMatrix(vals);
  45. System.out.println(" ");
  46. rotation90Droite(vals);
  47. printMatrix(vals);
  48.  
  49. }
  50. }
Success #stdin #stdout 0.09s 27880KB
stdin
Standard input is empty
stdout
1 2 3 4  
5 6 7 8  
9 10 11 12  
13 14 15 16  
 
13 9 5 1  
14 10 6 2  
15 11 7 3  
16 12 8 4