Java、最大的行和列

Java、最大的行和列_第1张图片

 


package pack1;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Scanner;

public class MostRowAndColumn {
    public static void main(String[] args) {
        try(Scanner input = new Scanner(System.in);) {
            System.out.print("Enter the array size n: ");
            int[][] matrix = getRandomMatrix(input.nextInt());

            System.out.println("The random array is ");
            printMatrix(matrix);

            System.out.println("The largest row index: " + 
                     getMostRowIndex(matrix).toString().replaceAll("[\\[\\]]", ""));
            System.out.println("The largest column index: " + 
                     getMostColumnIndex(matrix).toString().replaceAll("[\\[\\]]", ""));
        }
    }

    /**返回随机矩阵*/
    public static int[][] getRandomMatrix(int n) {
        int[][] matrix = new int[n][n];

        for (int i = 0; i < matrix.length; i++)
            for (int j = 0; j < matrix[i].length; j++)
                matrix[i][j] = new SecureRandom().nextInt(2);
        return matrix;
    }

    /**打印矩阵*/
    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++)
                System.out.print(matrix[i][j] + " ");
            System.out.println();
        }
    }

    /**返回最多1的行的数组列表*/
    public static ArrayList getMostRowIndex(int[][] matrix) {
        if(matrix == null) return null; //如果空引用,返回null

        ArrayList row = new ArrayList<>(matrix.length);
        for (int i = 0; i < matrix.length; i++)
            row.add(0); //row初始matrix.length个0

        for (int i = 0; i < matrix.length; i++)
            for (int j = 0; j < matrix[i].length; j++)
                row.set(i, row.get(i) + matrix[i][j]);  //row.get(i)为第i行的和

        int sum = row.get(0);   //最多1的值
        ArrayList indexSet = new ArrayList<>(matrix.length);   //下标集
        for (int i = 1; i < row.size(); i++)
            if (sum < row.get(i))   //获取最多1的值
                sum = row.get(i);
        for (int i = 0; i < row.size(); i++)
            if(sum == row.get(i))   //获取下标
                indexSet.add(i);
        return indexSet;
    }

    /**返回最多1的列的数组列表*/
    public static ArrayList getMostColumnIndex(int[][] matrix) {
        if(matrix == null) return null;

        ArrayList column = new ArrayList<>(matrix.length);
        for (int i = 0; i < matrix.length; i++)
            column.add(0);

        for (int i = 0; i < matrix.length; i++)
            for (int j = 0; j < matrix[i].length; j++)
                column.set(i, column.get(i) + matrix[j][i]);

        int sum = column.get(0);
        ArrayList indexSet = new ArrayList<>(matrix.length);
        for (int i = 1; i < column.size(); i++)
            if (sum < column.get(i))
                sum = column.get(i);
        for (int i = 0; i < column.size(); i++)
            if(sum == column.get(i))
                indexSet.add(i);
        return indexSet;
    }
}

Java、最大的行和列_第2张图片

 

你可能感兴趣的:(java,蓝桥杯,算法)