当先锋百科网

首页 1 2 3 4 5 6 7

已知有一个NXN的矩阵,

要求: 

1、查找该矩阵中最大值的位置, 并输出该位置 (1分)

2、求该矩阵的对角线之积,并输出该值  (2分)

3、实现该矩阵的转置,并输出转置后的矩阵 (2分)

import java.util.Scanner;
public class try1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入矩阵的大小(行列)");
        int M = scanner.nextInt();
        int N = scanner.nextInt();
        int num[][] = new int[M][N];
        int i, j;
        int k = 1;
        int max, maxi, maxj;
        System.out.println("请输入矩阵内"+M * N+"个数");
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++)
                num[i][j] = scanner.nextInt();
        }
        max = num[0][0];
        maxi = maxj = 0;
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++) {
                if (i==j){
                    k *= num[i][j];
                }
                if (num[i][j] > max) {
                    max = num[i][j];
                    maxi = i;
                    maxj = j;
                }
            }
        }
        System.out.println("最大值为" + max + "位置:第" + (maxi+1) + "行第" + (maxj+1)+ "列");
        System.out.println("对角线乘积为" + k);
        System.out.println("转置后的矩阵为");
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++) {
                System.out.print(num[j][i]+"\t");
            }
            System.out.println();
        }
    }
}