📜  接受 M x N 阶矩阵并交换对角线的Java程序

📅  最后修改于: 2022-05-13 01:55:33.324000             🧑  作者: Mango

接受 M x N 阶矩阵并交换对角线的Java程序

问题描述:编写一个Java程序,接受一个M × N阶矩阵,然后交换矩阵的对角线。

脚步:

1. 我们只能交换方阵的对角线。

2. 创建大小为 [M × M] 的方阵。

3. 检查矩阵是否为方阵。如果矩阵是方阵,则按照步骤 3 否则终止程序。

4. 对矩阵的交换对角线应用逻辑,下面给出了一些逻辑。

方法一:交换元素a[i][i]a[i][n – i -1]

例子:

Java
//  Java Program to Accept a Matrix of Order M x N &
//  Interchange the Diagonals
 
import java.util.Scanner;
public class InterchangeDiagonals {
    public static void main(String[] args)
    {
        // declare variable
        int m, n, i, j, temp;
 
        // create a object of scanner class
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows ");
 
        // take number of rows
        m = sc.nextInt();
 
        System.out.print("Enter number of columns ");
 
        // take number of columns
        n = sc.nextInt();
 
        // declare a mxn order array
        int a[][] = new int[m][n];
 
        // if block it's execute when m is equals to n
        if (m == n) {
            System.out.println(
                "Enter all the values of matrix ");
 
            // take the matrix inputs
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
 
            System.out.println("original Matrix:");
 
            // print the original matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println("");
            }
 
            // perform interchange
            for (j = 0; j < m; j++) {
                temp = a[j][j];
                a[j][j] = a[j][n - 1 - j];
                a[j][n - 1 - j] = temp;
            }
            System.out.println(
                " after interchanging diagonals of matrix ");
 
            // print interchanged matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println("");
            }
        }
 
        // else block it's only execute when m is not equals
        // to n
        else {
            System.out.println("Rows not equal to columns");
        }
    }
}


Java
//  Java Program to Accept a Matrix of Order MxN &
//  Interchange the Diagonals
 
import java.util.Scanner;
public class InterchangeDiagonals {
    public static void main(String[] args)
    {
        // declare variable
        int m, n, i, j, temp;
 
        // create a object of scanner class
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows ");
 
        // take number of rows
        m = sc.nextInt();
 
        System.out.print("Enter number of columns ");
 
        // take number of columns
        n = sc.nextInt();
 
        // declare a mxn order array
        int a[][] = new int[m][n];
 
        // if block it's execute when m is equals to n
        if (m == n) {
            System.out.println(
                "Enter all the values of matrix ");
 
            // take input matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
 
            System.out.println("original Matrix:");
 
            // print original matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println("");
            }
 
            // performing interchange
            for (j = 0; j < m; j++) {
                temp = a[j][j];
                a[j][j] = a[j][n - 1 - j];
                a[j][n - 1 - j] = temp;
            }
            System.out.println(
                " after interchanging diagonals of matrix ");
 
            // print interchanged matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println("");
            }
        }
 
        // else block it's only execute when m is not equals
        // to n
        else {
            System.out.println("Rows not equal to columns");
        }
    }
}



输出:

示例 2:

Java

//  Java Program to Accept a Matrix of Order MxN &
//  Interchange the Diagonals
 
import java.util.Scanner;
public class InterchangeDiagonals {
    public static void main(String[] args)
    {
        // declare variable
        int m, n, i, j, temp;
 
        // create a object of scanner class
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows ");
 
        // take number of rows
        m = sc.nextInt();
 
        System.out.print("Enter number of columns ");
 
        // take number of columns
        n = sc.nextInt();
 
        // declare a mxn order array
        int a[][] = new int[m][n];
 
        // if block it's execute when m is equals to n
        if (m == n) {
            System.out.println(
                "Enter all the values of matrix ");
 
            // take input matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
 
            System.out.println("original Matrix:");
 
            // print original matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println("");
            }
 
            // performing interchange
            for (j = 0; j < m; j++) {
                temp = a[j][j];
                a[j][j] = a[j][n - 1 - j];
                a[j][n - 1 - j] = temp;
            }
            System.out.println(
                " after interchanging diagonals of matrix ");
 
            // print interchanged matrix
            for (i = 0; i < m; i++) {
                for (j = 0; j < n; j++) {
                    System.out.print(a[i][j] + " ");
                }
                System.out.println("");
            }
        }
 
        // else block it's only execute when m is not equals
        // to n
        else {
            System.out.println("Rows not equal to columns");
        }
    }
}

输出: