2017-10-02 3 views
-1
import java.io.*; 
import java.util.*; 

public class Solution { 

    public static void main(String[] args) { 
     /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 
     int sum1=0, sum2=0; 
     Scanner in= new Scanner(System.in); 
     int rows = in.nextInt(); 
     int[][] matrix= new int[rows][rows]; 
     for(int i=0; i<rows; i++) 
     { 
      for(int j=0; j<rows; j++) 
      { 
       matrix[i][j]=in.nextInt(); 
       if(i==j) 
       { 
        sum1=sum1+matrix[i][j]; 
       } 
       else if((i+j)%2==0) 
       { 
        sum2=sum2+matrix[i][j]; 
       } 
      } 
     } 
     System.out.print(Math.abs(sum2-sum1)); 
    } 
} 

Ce code permet de connaître la différence de diagonale. Mais cela ne fonctionne pas correctement. Quelqu'un peut-il m'aider à corriger ce code?Le code de différence diagonale ne fonctionne pas

+0

Pouvez-vous relier le lien HackerRank? Aussi, juste une conjecture sauvage, avec cette ligne de code: "int rows = in.nextInt();" manque-t-il une autre ligne appelée "int columns = in.nextInt();"? Je suppose qu'il vous fournit une autre variable dans l'entrée, mais je peux me tromper puisque je ne suis pas sûr de savoir à quel défi HackerRank vous faites référence. – retodaredevil

+0

Non, je pense que OP n'a pas besoin de 'columns', comme:' int [] [] matrix = new int [lignes] [lignes]; '... –

+0

En d'autres termes, c'est une matrice carrée. 'rows' pourrait probablement être renommé' size'. – markspace

Répondre

0

Vous pouvez modifier la condition lorsque vous évaluez la sum2 de:

else if((i+j)%2==0) //e.g [2,2] would also be included in this sum otherwise 

à

else if((i+j) == rows-1) 
// the inverse diagonal holds that the sum of its indexes would be equal to the size of the square matrix 
0

je l'ai modifié un peu, mais les principaux problèmes sont les suivants:

  • Vous avez incorrectement calculé la deuxième diagonale.
  • Le else entre les deux instructions if empêchait également les matrices de taille impaire d'ajouter la valeur moyenne à la deuxième diagonale.

Ainsi, il devrait être quelque chose comme ceci:

public static void main(String[] args) 
{ 
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ 
    try (Scanner in = new Scanner(System.in)) 
    { 
     int sum1 = 0, sum2 = 0; 
     System.out.print("Enter size: "); 
     int rows = in.nextInt(); 
     in.nextLine(); 
     System.out.println("Enter matrix:"); 
     int[][] matrix = new int[rows][rows]; 
     for (int i = 0 ; i < rows ; i++) 
     { 
      for (int j = 0 ; j < rows ; j++) 
      { 
       matrix[i][j] = in.nextInt(); 
       if (i == j) 
       { 
        System.out.printf("i = j = %d, [i,j] = %d%n", i, matrix[i][j]); 
        sum1 = sum1 + matrix[i][j]; 
       } 
       if (i + j == rows - 1) 
       { 
        System.out.printf("i = %d, j = %d, [i,j] = %d%n", i, j, matrix[i][j]); 
        sum2 = sum2 + matrix[i][j]; 
       } 
      } 
     } 
     System.out.printf("%nsum1 = %d, sum2 = %d, difference = %d%n", sum1, sum2, 
          Math.abs(sum2 - sum1)); 
    } 
} 
+0

vous avez obtenu par quelques secondes;) – nullpointer

0
int sum1 = 0, sum2 = 0; //let sum1 and sum2 be your sum of diagonals 

    for (int i = 0; i < n; i++)// let n be the size of the matrix 
    { 
     for (int j = 0; j < n; j++) 
     { 
      // finding sum of primary diagonal 
      if (i == j) 
       d1 += arr[i][j]; 
//finding sum of secondary diagonal 
if (i == n - j - 1) 
       d2 += arr[i][j]; 
     } 
    } 

    // Absolute difference of the sums 
    // across the diagonals 
    return abs(d1 - d2);