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编程语言找到所有行和所有列之间的差异。