在Java中查找所有行与所有列的和的差值
在Java中,矩阵可以用二维数组表示。矩阵用于存储和操作具有表格结构的数据。矩阵具有与其相关的多个属性和运算,例如加法、减法、乘法、转置和行列式计算。
根据题意,我们需要对所有单个行和列求和。然后我们需要计算这些行和列的和的差值,并显示结果。
让我们开始吧!
例如
假设原始矩阵为
{4, 2, 1},
{3, 5, 6},
{8, 9, 7}
对矩阵进行运算后,结果将为
所有行之和与所有列之和的差值是:0
算法
步骤 1:定义并初始化矩阵。
步骤 2:计算每一行的和,并将结果存储在一个数组中。
步骤 3:计算每一列的和,并将结果存储在一个数组中。
步骤 4:计算所有行的和以及所有列的和。
步骤 5:求所有行的和与所有列的和的差值。
步骤 6:打印结果。
多种方法
我们提供了多种方法的解决方案。
使用矩阵元素的静态初始化
使用用户自定义方法
让我们逐一查看程序及其输出。
方法 1:使用数组元素的静态初始化
在这种方法中,矩阵元素将在程序中初始化。然后根据算法查找所有行和所有列的和的差值。
示例
public class Main
{
public static void main(String[] args)
{
// Define and initialize the matrix
int[][] matrix = {{11, 7, 1},{15, 8, 6},{2, 5, 17}};
// Calculate the sum of each row and store the results in an array
int[] rowSums = new int[matrix.length];
for (int i = 0; i < matrix.length; i++) {
int sum = 0;
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
rowSums[i] = sum;
}
// Calculate the sum of each column and store the results in an array
int[] colSums = new int[matrix[0].length];
for (int j = 0; j < matrix[0].length; j++) {
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
sum += matrix[i][j];
}
colSums[j] = sum;
}
// sum of all rows
int sumOfRows = 0;
for (int sum : rowSums) {
sumOfRows += sum;
}
// sum of all columns
int sumOfCols = 0;
for (int sum : colSums) {
sumOfCols += sum;
}
// Finding the difference between the sum of all rows and the sum of all columns
int difference = sumOfRows - sumOfCols;
// Display the difference
System.out.println("Difference between sum of all rows and all columns is: " + difference);
}
}
输出
Difference between sum of all rows and all columns is: 0
方法 2:使用用户自定义方法
在这种方法中,矩阵元素将在程序中初始化。然后通过将矩阵作为参数传递给用户自定义方法,并在方法内部根据算法查找所有行和所有列的和的差值。
示例
public class Main
{
public static void main(String[] args)
{
// Define and initialize the matrix
int[][] matrix = {{11, 7, 1},{15, 8, 6},{2, 5, 17}};
// calling user defined method
callingMatrix(matrix);
}
// user defined method
public static void callingMatrix(int[][] matrix)
{
// Calculate the sum of each row and store the results in an array
int[] rowSums = new int[matrix.length];
for (int i = 0; i < matrix.length; i++) {
int sum = 0;
for (int j = 0; j < matrix[i].length; j++) {
sum += matrix[i][j];
}
rowSums[i] = sum;
}
// Calculate the sum of each column and store the results in an array
int[] colSums = new int[matrix[0].length];
for (int j = 0; j < matrix[0].length; j++) {
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
sum += matrix[i][j];
}
colSums[j] = sum;
}
// sum of all rows
int sumOfRows = 0;
for (int sum : rowSums) {
sumOfRows += sum;
}
// sum of all columns
int sumOfCols = 0;
for (int sum : colSums) {
sumOfCols += sum;
}
// Finding the difference between the sum of all rows and the sum of all columns
int difference = sumOfRows - sumOfCols;
// Display the difference
System.out.println("Difference between sum of all rows and all columns is: " + difference);
}
}
输出
Difference between sum of all rows and all columns is: 0
在本文中,我们探讨了如何使用 Java 编程语言查找所有行和所有列的和的差值。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP