Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.2 kB
7
Indexable
Never
import java.util.Random;
import java.util.Scanner;

public class Task {
    private static final Random RND = new Random();
    public static int[] createRandomIntArray(int length, int minValue, int maxValue) {
        int[] arr = new int[length];
        for (int i = 0; i < length; i++)
            arr[i] = minValue + RND.nextInt(maxValue - minValue);
        return arr;
    }
    public static int[][] createRandomIntMatrix(int rowCount, int colCount, int minValue, int maxValue) {
//        int [][] matrix = {{5,3,2,2}, {8,3,4,4}, {7,2,3,0}, {4,2,3,1}}; //{Тест, который не проходит проверку}
        //{Рандомная матрица}
       int[][] matrix = new int[rowCount][];
       for (int r = 0; r < rowCount; r++) 
           matrix[r] = createRandomIntArray(colCount, minValue, maxValue);      
       return matrix;
    }
    public static void swapColumns(int[][] arr2, int col1, int col2) {
    for (int[] row : arr2) {
            int temp = row[col1];
            row[col1] = row[col2];
            row[col2] = temp;
        }
    }
    public static void arraySort(int[][] a) {
        int zero = 0;
        for (int row = 0; row < a.length - 1; row++) {
            for (int col = 0; col < a.length - 1; col++) {
                if (a[zero][col] > a[zero][col + 1]) {
                    swapColumns(a, col, col + 1);

                } else if (a[zero][col] == a[zero][col + 1]) {
                    for (int i = 0; i < a.length - 1; i++) {
                        if (a[zero + 1][col] > a[zero + 1][col + 1]) {
                            swapColumns(a, col, col + 1);
                        }
                        else if (a[zero + 1][col] == a[zero + 1][col + 1]){
                            System.out.println("test.text");
                            zero++;
                            break;
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Введите кол-во строк: ");
        int rowCount = scanner.nextInt();
        System.out.print("Введите кол-во cтолбцов: ");
        int colCount = scanner.nextInt();
        System.out.print("Введите минимальное значение: ");
        int minValue = scanner.nextInt();
        System.out.print("Введите максимальное значение: ");
        int maxValue = scanner.nextInt();

        int[][] a = createRandomIntMatrix(rowCount, colCount, minValue, maxValue);
        for (int[] ints : a) {
            for (int j = 0; j < a.length; j++) {
                System.out.print(ints[j] + " ");
            }
            System.out.print("\n");
        }
        for(int i = 0; i < rowCount; i++) {
            arraySort(a);
        }
        System.out.println("------------------------------------------");
        for (int[] ints : a) {
            for (int j = 0; j < a.length; j++) {
                System.out.print(ints[j] + " ");
            }
            System.out.print("\n");
        }
    }
}