接受 M x N 阶矩阵并交换对角线的Java程序
问题描述:编写一个Java程序,接受一个M × N阶矩阵,然后交换矩阵的对角线。
脚步:
1. 我们只能交换方阵的对角线。
2. 创建大小为 [M × M] 的方阵。
3. 检查矩阵是否为方阵。如果矩阵是方阵,则按照步骤 3 否则终止程序。
4. 对矩阵的交换对角线应用逻辑,下面给出了一些逻辑。
方法一:交换元素a[i][i]和a[i][n – i -1]
for (j = 0; j < m; j++) {
temp = a[j][j];
a[j][j] = a[j][n – 1 – j];
a[j][n – 1 – j] = temp;
}
例子:
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");
}
}
}
输出:
Enter number of rows 3
Enter number of columns 3
Enter all the values of matrix
1
2
3
4
5
6
7
8
9
Original Matrix:
1 2 3
4 5 6
7 8 9
After interchanging diagonals of matrix
3 2 1
4 5 6
9 8 7
示例 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");
}
}
}
输出:
Enter number of rows 2
Enter number of columns 1
Enter all the values of matrix
1
2
Rows not equal to columns