This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| algorithm:algorithm [2020/01/16 11:44] – Tag0 Removed: Algorithm ledyx | algorithm:algorithm [2022/10/24 15:32] (current) – ledyx | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| = Algorithm = | = Algorithm = | ||
| - | {{tag> | + | {{tag>Algorithm}} |
| - | + | ||
| - | = 마방진 (Magic Square) = | + | |
| - | * 홀수만 처리 가능! | + | |
| - | <sxh java ; title: | + | |
| - | int size = 5; | + | |
| - | + | ||
| - | int[][] arr = new int[size][size]; | + | |
| - | + | ||
| - | int middle = size/2; | + | |
| - | + | ||
| - | int i=0, j=middle; | + | |
| - | for(int num=1 ; num< | + | |
| - | arr[i][j] = num; | + | |
| - | + | ||
| - | //행 감소 | + | |
| - | i--; | + | |
| - | if(i < 0) | + | |
| - | i = size-1; | + | |
| - | + | ||
| - | //열 증가 | + | |
| - | j = (++j)%size; | + | |
| - | //아래 표현과 같다. | + | |
| - | /*j++; | + | |
| - | if(j >= size) | + | |
| - | j = 0;*/ | + | |
| - | + | ||
| - | // | + | |
| - | if(num%size == 0) { | + | |
| - | i = (i+2)%size; | + | |
| - | j--; | + | |
| - | if(j < 0) | + | |
| - | j = size-1; | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| = Recursion = | = Recursion = | ||
| Line 54: | Line 19: | ||
| [[binary search|참조]] | [[binary search|참조]] | ||
| - | |||
| - | = Dynamic Programming = | ||
| - | <sxh java> | ||
| - | private TreeSet< | ||
| - | if (!srcDir.isFile() && srcDir.listFiles() == null) | ||
| - | throw new FileNotFoundException(" | ||
| - | |||
| - | // 파일 이름순 정렬 | ||
| - | TreeSet< | ||
| - | public int compare(File o1, File o2) { | ||
| - | return o1.getPath().compareToIgnoreCase(o2.getPath()); | ||
| - | } | ||
| - | }); | ||
| - | |||
| - | Stack< | ||
| - | directories.push(srcDir); | ||
| - | |||
| - | // DFS | ||
| - | while(!directories.isEmpty()) { | ||
| - | File directory = directories.pop(); | ||
| - | |||
| - | for(File composite : directory.listFiles()) { | ||
| - | if(composite.isDirectory()) { | ||
| - | directories.push(composite); | ||
| - | } | ||
| - | else if(composite.isFile()) { | ||
| - | files.add(composite); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | |||
| - | return files; | ||
| - | } | ||
| - | </ | ||