스터디사이트 : https://www.hackerrank.com



JAVA 알고리즘 : 이차원 배열 각 대각선 합의 차이값 구하기(Diagonal Difference)


※ 입력 방법

첫 줄은 2차원배열의 N x N 배열의 N값을 입력하는 곳

나머지는 2차원 배열의 값을 입력.


※ 계산 방법

첫번째 대각선 : 

11

5

-12

11 + 5 - 12 = 4

두번째 대각선 : 

4

5

10

10 + 5 + 4 = 19


각 대각선 합의 차 : 

| 4 - 19 | = 15


입력

3

11 2 4

4 5 6

10 8 -12


출력

15


코딩 - 1

우선 이차원 배열의 크기 (N x N) 을 출력시키고, 그 배열에 들어가야할 데이터값 갯수를 구해봤다..

그리고 반복문을 이용해서 데이터값 갯수만큼 반복시키게 하고,

그 안에서 입력값을 하나씩 집어넣게 하는 ArrayList 클래스변수를 선언했다.

public static void main(String[] args) {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner scan = new Scanner(System.in);

        int inputsize = scan.nextInt();

           System.out.println(inputsize);

        int arraysize = inputsize * inputsize;

           System.out.println("arraysize : " + arraysize);


        List<Integer> arraydata = new ArrayList<Integer>();

}

결과 : 

3

arraysize : 9


코딩 - 2

반복문을 이용해서 데이터값 갯수만큼 반복시키게 하고,

그 안에서 ArrayList.add() 를 써서 입력값을 다 집어넣었다.


이제 대각선의 위치를 구하는 게 남았네?

public static void main(String[] args) {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner scan = new Scanner(System.in);

        int inputsize = scan.nextInt();

            System.out.println(inputsize);

        int arraysize = inputsize * inputsize;

            System.out.println("arraysize : " + arraysize);

        List<Integer> arraydata = new ArrayList<Integer>();

        

        for(int i=0; i<arraysize; i++){

            arraydata.add(scan.nextInt());

            System.out.println("arraydata[" + i +"] : " + arraydata.get(i));

        }

}

결과 : 

3

arraysize : 9

arraydata[0] : 11

arraydata[1] : 2

arraydata[2] : 4

arraydata[3] : 4

arraydata[4] : 5

arraydata[5] : 6

arraydata[6] : 10

arraydata[7] : 8

arraydata[8] : -12


코딩 - 3

내 나름대로 대각선의 위치를 구하는 로직을 짜봤다.. 

ex) 3 x 3 배열

첫번째 대각선 - 

        1행 : 0*3(행) + 1 = 1

        2행 : 1*3(행) + 2 = 5

        3행 : 2*3(행) + 3 = 9


두번째 대각선 - 

        1행 : 0*3(행) + (3-0) = 3

        2행 : 1*3(행) + (3-1) = 5

        3행 : 2*3(행) + (3-2) = 7


N x N 배열이라 했으므로,

for문을 돌릴때, i 를 0부터 N까지 1씩 증가시킬 때 나오는 공식은 이렇게 하면 될 것 같다.

i * N + (i+1)


그리고 반대편 대각선 공식은 행은 똑같이 1행부터 증가시키면서, 배열의 (열사이즈) - (반복문의 i) 를 해 나가면 되겠구나!

i * N + (N-i)


ArrayList에 저장되어 있는 값을 불러올 때,

인덱스가 0부터 시작하므로, (대각선 위치 - 1) 을 해서 불러온다.


public static void main(String[] args) {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner scan = new Scanner(System.in);

        int inputsize = scan.nextInt();

            System.out.println(inputsize);

        int arraysize = inputsize * inputsize;

            System.out.println("arraysize : " + arraysize);

        List<Integer> arraydata = new ArrayList<Integer>();

        

        for(int i=0; i<arraysize; i++){

            arraydata.add(scan.nextInt());            

        }

        /*

        ex) 3 x 3 배열

        1행 : 1 = 0*3(행) + 1 = 1

        2행 : 2 = 1*3(행) + 2 = 5

        3행 : 3 = 2*3(행) + 3 = 9

        */

        int diagonal_1;         /* 첫번째 대각선위치값 저장하는 변수 */

        int diagonal_2;         /* 두번째 대각선위치값 저장하는 변수 */

        int sum_diagonal_1=0;         /* 첫번째 대각선값들 합계 저장하는 변수 */

        int sum_diagonal_2=0;         /* 두번째 대각선값들 합계 저장하는 변수 */

        for(int j=0; j<inputsize; j++){

            diagonal_1 = j*inputsize + (j+1);

            diagonal_2 = j*inputsize + (inputsize-j);

                System.out.print("첫번째 대각선 위치 : " + diagonal_1);

                System.out.println(" 의 값 : " + arraydata.get(diagonal_1-1));

                System.out.print("두번째 대각선 위치 : " + diagonal_2);

                System.out.println(" 의 값 : " + arraydata.get(diagonal_2-1));

            sum_diagonal_1 = sum_diagonal_1+arraydata.get(diagonal_1-1);

            sum_diagonal_2 = sum_diagonal_2+arraydata.get(diagonal_2-1);

        }

        System.out.println("1 대각선 합 : " + sum_diagonal_1);

        System.out.println("2 대각선 합 : " + sum_diagonal_2);

}

결과 : 

3

arraysize : 9

첫번째 대각선 위치 : 1 의 값 : 11

  두번째 대각선 위치 : 3 의 값 : 4

첫번째 대각선 위치 : 5 의 값 : 5

  두번째 대각선 위치 : 5 의 값 : 5

첫번째 대각선 위치 : 9 의 값 : -12

  두번째 대각선 위치 : 7 의 값 : 10

1 대각선 합 : 4

  2 대각선 합 : 19



결과

절대값을 구하는 메소드는

Math.abs();

이고, 인자에다가 Integer 값을 넣어주면 된다.


그러면 실제 적용할 때 이렇게 적용시킨다.

Math.abs(sum_diagonal_1 - sum_diagonal_2);

public static void main(String[] args) {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

        Scanner scan = new Scanner(System.in);

        int inputsize = scan.nextInt();

        int arraysize = inputsize * inputsize;

        List<Integer> arraydata = new ArrayList<Integer>();

        

        for(int i=0; i<arraysize; i++){

            arraydata.add(scan.nextInt());            

        }

        /*

        ex) 3 x 3 배열

        1행 : 1 = 0*3(행) + 1 = 1

        2행 : 2 = 1*3(행) + 2 = 5

        3행 : 3 = 2*3(행) + 3 = 9

        */

        int diagonal_1;         /* 첫번째 대각선위치값 저장하는 변수 */

        int diagonal_2;         /* 두번째 대각선위치값 저장하는 변수 */

        

        int sum_diagonal_1=0;         /* 첫번째 대각선값들 합계 저장하는 변수 */

        int sum_diagonal_2=0;         /* 두번째 대각선값들 합계 저장하는 변수 */

        for(int j=0; j<inputsize; j++){

            diagonal_1 = j*inputsize + (j+1);

            diagonal_2 = j*inputsize + (inputsize-j);


            sum_diagonal_1 = sum_diagonal_1+arraydata.get(diagonal_1-1);

            sum_diagonal_2 = sum_diagonal_2+arraydata.get(diagonal_2-1);

        }

        System.out.println(Math.abs(sum_diagonal_1-sum_diagonal_2));

}

결과 : 


15


+ Recent posts