第八章第十四题(探讨矩阵)(Discussion matrix)
-
**8.14(探讨矩阵)编写程序提示用户输入一个方阵的长度,随机的在矩阵中填入0和1,打印这个矩阵,然后找到,整行,整列或者对角线都是0或1的行、列和对角线。下面是这个程序的一个运行示例:
Enter the size for the matrix: 4
0 1 0 0
1 0 0 0
0 0 0 1
0 0 1 1
no same numbers on a row
no same numbers on a column
Na same numbers on the major diagonal
All 0s on the sub-diagonal
**8.14(Discussion matrix)Write a program to prompt the user to enter the length of a square matrix, randomly fill in 0 and 1 in the matrix, print the matrix, and then find the row, column and diagonal line with 0 or 1 as the whole row, column or diagonal. Here is a running example of this program:
Enter the size for the matrix: 4
0 1 0 0
1 0 0 0
0 0 0 1
0 0 1 1
no same numbers on a row
no same numbers on a column
Na same numbers on the major diagonal
All 0s on the sub-diagonal -
参考代码:
package chapter08; import java.util.Scanner; public class Code_14 { public static void main(String args[]){ Scanner cin = new Scanner(System.in); System.out.print("Enter the size for the matrix: "); int size = cin.nextInt(); int[][] matrix = new int[size][size]; for(int i = 0; i < size; ++i){ for(int j = 0; j < size; ++j){ matrix[i][j] = (((int)System.currentTimeMillis()) / (i