用java实现矩阵中出现最多1的行和列(只有0,1)

问题描述

编写一个程序,在一个4*4的矩阵中随机填入0和1,打印该矩阵,找到第一个具有最多1的行和列。

代码

public class Program10 {
	public static void main(String[] args) {
		int[][] a = new int[4][4];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a.length; j++) {
				a[i][j] = (int) (Math.random() * 2);
				System.out.print(a[i][j]);
			}
			System.out.println();
		}
		System.out.println("The largest row index:" + max(a, true));
		System.out.println("the largest column index:" + max(a, false));

	}

	private static int max(int[][] array, boolean rowCol) {
		int max;
		int temp = 0, cou = -1;
		for (int i = 0; i < array.length; i++) {
			max = 0;
			for (int j = 0; j < array[i].length; j++) {
				int index = (rowCol) ? array[i][j] : array[j][i];
				if (index == 1)
					max++;
			}
			if (temp < max) {
				temp = max;
				cou = i;
			}
		}
		return cou;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40435621/article/details/83759354