如何计算对角线之和的绝对差?

huangapple go评论127阅读模式
英文:

How to calculate the absolute difference between the sum of diagonals?

问题

public class DiagonalDifference {
    public static int calculateDiagonalDifference(int[][] matrix) {
        int n = matrix.length;
        int leftToRightSum = 0;
        int rightToLeftSum = 0;

        for (int i = 0; i < n; i++) {
            leftToRightSum += matrix[i][i];
            rightToLeftSum += matrix[i][n - i - 1];
        }

        return Math.abs(leftToRightSum - rightToLeftSum);
    }

    public static void main(String[] args) {
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {9, 8, 9}
        };

        int result = calculateDiagonalDifference(matrix);
        System.out.println("The absolute difference between diagonals is: " + result);
    }
}
英文:

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

For example, the square matrix is shown below:

1 2 3
4 5 6
9 8 9  

The left-to-right diagonal =15 . The right to left diagonal = 17. Their absolute difference is 2.
Please help with the code for Java.

答案1

得分: 1

考虑一个大小为 N*N 的数组矩阵。从左到右的对角线与行、列具有相同的索引。从右到左的对角线的索引之和为 N-1。一旦将它们相加,您可以减去它们并对其取绝对值:

int[][] matrix = ...; // 

int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < matrix.length; ++i) {
    sum1 += matrix[i][i];
    sum2 += matrix[i][matrix.length - i - 1];
}
int result = Math.abs(sum1 - sum2);
英文:

Consider a matrix as an array of arrays of the size N*N. The left-to-right diagonal the row and the column have the same index. On the right to left diagonal the indexes sum up to N-1. Once you sum them, you can then subtract them and apply the absolute value to it:

int[][] matrix = ...; // 

int sum1 = 0;
int sum2 = 0;
for (int i = 0; i &lt; matrix.length; ++i) {
    sum1 += matrix[i][i];
    sum2 += matrix[i][matrix.length - i - 1];
}
int result = Math.abs(sum1 - sum2);

答案2

得分: 1

public int diagonalDifferences(List<List<Integer>> list) {

    int rSum = 0;
    int lSum = 0;
    int i = 0;
    int j = 0;
    int dif = 0;
    for(i=0; i<list.size(); i++){
        for(j=0; j<list.get(0).size(); j++){
            if(i==j){
                lSum +=list.get(i).get(j);
            }
            if((i+j) == (list.get(0).size()-1)){
                rSum +=list.get(i).get(j);
            }
        }
    }

    dif = Math.abs(rSum - lSum);
    return dif;
}
英文:
 public int diagonalDifferences(List&lt;List&lt;Integer&gt;&gt; list) {

    int rSum = 0;
    int lSum = 0;
    int i = 0;
    int j = 0;
    int dif = 0;
    for(i=0; i&lt;list.size(); i++){
        for(j=0; j&lt;list.get(0).size(); j++){
            if(i==j){
                lSum +=list.get(i).get(j);
            }
            if((i+j) == (list.get(0).size()-1)){
                rSum +=list.get(i).get(j);
            }
        }
    }
    
    dif = Math.abs(rSum - lSum);
    return dif;
}

答案3

得分: 0

import java.util.*;

public class Main {
    static int diagonalDiff(int arr[][]) {
        int leftSum = 0;
        int rightSum = 0;
        int n = arr.length;
        for (int i = 0; i < arr.length - 1; i++) {
            leftSum = leftSum + arr[i][i];
            rightSum = rightSum + arr[i][n - 1 - i];
        }
        return java.lang.Math.abs(leftSum - rightSum);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter size");
        int n = scan.nextInt();
        int a[][] = new int[n][n];
        System.out.print("Enter array elements");
        for (int i = 0; i < a.length - 1; i++) {
            for (int j = 0; j <= a[i].length - 1; j++) {
                a[i][j] = scan.nextInt();
            }
        }
        System.out.print(diagonalDiff(a));
    }
}
英文:
import java.util.*;
public class Main
{
static int diagonalDiff(int arr[][])
{
    int leftSum=0;
    int rightSum=0;
    int n=arr.length;
    for(int i=0;i&lt;arr.length-1;i++)
    {
        leftSum=leftSum+arr[i][i];
        rightSum=rightSum+arr[i][n-1-i];
    }
    return java.lang.Math.abs(leftSum-rightSum);
}
public static void main(String[] args)
{
    Scanner scan=new Scanner(System.in);
    System.out.print(&quot;Enter size&quot;);
    int n=scan.nextInt();
    int a[][]=new int[n][n];
    System.out.print(&quot;Enter array elements&quot;);
    for(int i=0;i&lt;a.length-1;i++)
    {
        for(int j=0;j&lt;=a[i].length-1;j++)
        {
            a[i][j]=scan.nextInt();
        }
    }
    System.out.print(diagonalDiff(a));
}
}

答案4

得分: -1

使用Math.abs()来获取对角线之间的绝对差异

链接:https://www.tutorialspoint.com/java/lang/math_abs_int.htm

英文:

Use Math.abs() to get the absolute difference between the diagonals

https://www.tutorialspoint.com/java/lang/math_abs_int.htm

huangapple
  • 本文由 发表于 2020年4月8日 21:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/61102495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定