Java 在二维数组中添加对角线值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6565862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding the diagonal values in a 2d array
提问by JDB
I have the following 2d array
我有以下二维数组
int [][] array = {{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
{20, 21, 22, 23, 24, 25, 26, 27, 28, 29},
{30, 31, 32, 33, 34, 35, 36, 37, 38, 39},
{40, 41, 42, 43, 44, 45, 46, 47, 48, 49},
{50, 51, 52, 53, 54, 55, 56, 57, 58, 59},
{60, 61, 62, 63, 64, 65, 66, 67, 68, 69},
{70, 71, 72, 73, 74, 75, 76, 77, 78, 79},
{80, 81, 82, 83, 84, 85, 86, 87, 88, 89},
{90, 91, 92, 93, 94, 95, 96, 97, 98, 99}};
I have this code to find the sum of all the values in the array. How can I modify it to add only the diagonal values starting at 0 (0+11+22+33 etc.)?
我有这个代码来查找数组中所有值的总和。如何修改它以仅添加从 0 开始的对角线值(0+11+22+33 等)?
public static int arraySum(int[][] array)
{
int total = 0;
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
total += array[row][col];
}
return total;
}
采纳答案by GSingh
Since the diagonals are at perfect square you only need one loop to add the diagonals.
由于对角线处于完美正方形,因此您只需要一个循环即可添加对角线。
Adding diagonal from orgin:
从原点添加对角线:
public static int arraySum(int[][] array){
int total = 0;
for (int row = 0; row < array.length; row++)
{
total += array[row][row];
}
return total;
}
Add both diagonals:
添加两条对角线:
Adding diagonal from orgin: (note it adds the center twice..you can subtract one if needed)
从原点添加对角线:(注意它添加了两次中心......如果需要,你可以减去一个)
public static int arraySum(int[][] array){
int total = 0;
for (int row = 0; row < array.length; row++)
{
total += array[row][row] + array[row][array.length - row-1];
}
return total;
}
回答by dave
public static int arraySum(int[][] array)
{
int total = 0;
for (int index = 0; index < array.length; index++)
{
total += array[index][index];
}
return total;
}
This of course assumes m x m for the dimensions.
这当然假设尺寸为 mxm。
回答by Sheela
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int leftStartDiagnol = 0;
int rightStartDiagnol = n;
int leftTotal = 0;
int rightTotal = 0;
int a[][] = new int[n][n];
for (int a_i = 0; a_i < n; a_i++) {
for (int a_j = 0; a_j < n; a_j++) {
a[a_i][a_j] = in.nextInt();
}
}
for (int a_i = 0; a_i < n; a_i++) {
boolean leftNotFound = true;
boolean rightNotFound = true;
rightStartDiagnol = --rightStartDiagnol;
for (int a_j = 0; a_j < n; a_j++) {
if (leftStartDiagnol == a_j && leftNotFound) {
leftTotal = leftTotal + a[a_i][a_j];
leftNotFound = false;
}
if (rightStartDiagnol == a_j && rightNotFound) {
rightTotal = rightTotal + a[a_i][a_j];
rightNotFound = false;
}
}
leftStartDiagnol = ++leftStartDiagnol;
}
int data = leftTotal - rightTotal;
System.out.println(Math.abs(data));
}
}
回答by geekintown
Here is my solution -
这是我的解决方案 -
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[][] = new int[n][n];
for(int a_i=0; a_i < n; a_i++){
for(int a_j=0; a_j < n; a_j++){
a[a_i][a_j] = in.nextInt();
}
}
int l_sum = 0;
for(int i = 0; i<n ; i++){
l_sum+=a[i][i];
}
int r_sum = 0;
for(int j = 0; j<n ; j++){
r_sum+=a[j][n-1-j];
}
int sum = l_sum + r_sum;
System.out.println(sum);
}
}
回答by geekintown
import java.io.*;
import java.util.*;
public class DigArraySum {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=Integer.parseInt(s.nextLine()); //array size
int[][] a=new int[n][n]; // new array
for (int i=0;i<n;i++) {
String str=s.nextLine();
String[] tempArray=str.split(" ");
for (int j=0;j<n;j++) {
a[i][j]=Integer.parseInt(tempArray[j]);
}
}
int x=0;//primary diagonal sum
int y=0;//secondary diagonal sum
int sum=0;//total sum
for (int i=0;i<n;i++) {
x += a[i][i];
}
for (int p=0;p<n;p++) {
int k=a.length-p-1;
y+=a[p][a.length-p-1];
k+=-1;
}
sum=x-y;
System.out.println(Math.abs(sum));
}
}
Explanation:for example 3*3 matrix: 3// Array size
说明:例如3*3矩阵:3//数组大小
11 2 4
11 2 4
4 5 6
4 5 6
10 8 -12
10 8 -12
The primary diagonal is: 11 5 -12
主对角线为:11 5 -12
Sum across the primary diagonal: 11 + 5 - 12 = 4
主对角线的总和:11 + 5 - 12 = 4
The secondary diagonal is: 4 5 10 Sum across the secondary diagonal: 4 + 5 + 10 = 19
次对角线为:4 5 10次对角线的总和:4 + 5 + 10 = 19
Total Sum: |4 + 19| = 23
总和:|4 + 19| = 23
回答by chamzz.dot
Here is my solution. Check out it.
这是我的解决方案。检查它。
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int a[][] = new int[n][n];
int total1=0;
int total2=0;
for(int a_i=0; a_i < n; a_i++){
for(int a_j=0; a_j < n; a_j++){
a[a_i][a_j] = in.nextInt();
}
total1+= a[a_i][a_i];
total2+=a[a_i][n-1-a_i];
}
System.out.println(Math.abs(total1-total2));
}
}
}
回答by Samundra
Solution in PHP. The logic is exactly the same what is already posted here. It's only in PHP that's different.
PHP中的解决方案。逻辑与这里已经发布的完全相同。只是在 PHP 中有所不同。
<?php
$handle = fopen ("php://stdin", "r");
function diagonalDifference($a) {
$sumA = [];
$sumB = [];
$n = count($a);
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($i === $j) {
$sumA[] = $a[$i][$j];
}
if ($i + $j == count($a) -1 ) {
$sumB[] = $a[$i][$j];
}
}
}
$sum1 = array_sum($sumA);
$sum2 = array_sum($sumB);
return abs($sum1 - $sum2);
}
fscanf($handle, "%i",$n);
$a = array();
for($a_i = 0; $a_i < $n; $a_i++) {
$a_temp = fgets($handle);
$a[] = explode(" ",$a_temp);
$a[$a_i] = array_map('intval', $a[$a_i]);
}
$result = diagonalDifference($a);
echo $result . "\n";
?>
回答by Rajan Peter
The solution is:
解决办法是:
int total = 0;
for (int row = 0; row < array.length; row++)
{
total += array[row][row] + array[row][array.length - row - 1];
}
System.out.println("FINAL ANSWER: " + total);
回答by Odatha Bandara
Here is the code which I did to find the sum of primary diagonal numbers and secondary diagonal numbers
这是我用来求主对角线数和次对角线数之和的代码
static int diagSum(int[][] arr) {
int pd=0;
int sd=0;
int sum=0;
for(int i=0;i<=arr.length-1;i++){
pd=pd+arr[i][i];
}
for (int k=0,l=arr.length-1; k<arr.length&&l>=0 ; k++,l--) {
sd=sd+arr[k][l];
}
sum=pd+sd;
return sum;
}
回答by naveen agrahari
If you want to add both diagonal in a 2d array then here is my program.
如果你想在二维数组中添加两条对角线,那么这是我的程序。
static int diagonalDifference(int[][] arr) {
int r = arr.length;
int sum1 = 0;
int sum2 = 0;
int j=0;
for(int i=0;i<r;i++){
sum1 = sum1 + arr[i][j];
j++;
}
j--;
for(int i=0;i<r;i++) {
sum2 = sum2 + arr[i][j];
j--;
}
int sum=sum1+sum2;
return sum;
}