fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. final int n = 8, m = 8;
  6. int[][] mas = new int[n][m];
  7.  
  8. // Example: generate random numbers as in your pattern
  9. for (int i = 0; i < n; i++)
  10. for (int j = 0; j < m; j++)
  11. mas[i][j] = (int)(Math.random() * 100 - 50);
  12.  
  13. // Swap max in every row with diagonal element
  14. for (int i = 0; i < n; i++) {
  15. int maxIdx = 0;
  16. for (int j = 1; j < m; j++)
  17. if (mas[i][j] > mas[i][maxIdx])
  18. maxIdx = j;
  19.  
  20. // Swap if max is not diagonal
  21. int temp = mas[i][i];
  22. mas[i][i] = mas[i][maxIdx];
  23. mas[i][maxIdx] = temp;
  24. }
  25.  
  26. // Print result
  27. for (int i = 0; i < n; i++) {
  28. for (int j = 0; j < m; j++)
  29. System.out.printf("%4d", mas[i][j]);
  30. System.out.println();
  31. }
  32. }
  33. }
Success #stdin #stdout 0.16s 55924KB
stdin
Standard input is empty
stdout
  43   8  34 -25  31  40 -42  10
  31  41  10   5  38  20  10  32
 -15   9  40 -41 -48   5   4  33
 -42  17  -6  36 -40  -4 -12 -49
 -31  23  15 -30  48  25 -44  19
 -33  20 -10  10 -44  33  15 -15
 -49  31 -22  21   7 -41  43 -39
 -47  38   1 -19   0  33 -42  41