lecture.js/algorithm.log

#006 열우선 배열

malda 2017. 7. 10. 19:48
# 문제)
5X5배열을 아래와 같은 열 우선 방식으로 값을 채워넣어라.

1
6
11
16
21
2
7
12
17
22
3
8
13
18
23
4
9
14
19
24
5
10
15
20
25


기존 행 우선하던 접근을 순서만 바꾸어주면 끝난다.
말그대로 먼저 컬럼을 접근 한 후 열 접근을 해주면 됨.

package dataStructure.prediction.array;

public class PreferRowArray {
   
    private int columnSize = 5;
    private int rowSize = 5;
    private int[][] array = new int[rowSize][columnSize];
   
    public void execute() {
        int number = 1;
        for(int col=0; col<columnSize; col++) {
            for(int row=0; row<rowSize; row++) {
                array[row][col] = number++;
            }
        }
    }
   
    public void print() {
        for(int row=0;row<rowSize;row++) {
            for(int col=0;col<columnSize;col++) {
                System.out.printf("%3d",array[row][col]);
            }
            System.out.println();
        }
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PreferRowArray preferRow = new PreferRowArray();
        preferRow.execute();
        preferRow.print();
    }

}