티스토리 뷰

PS/etc

알고리즘 풀이 [검색]

kingsubin 2020. 11. 30. 11:00
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
100
101
102
103
104
105
106
import java.io.*;
import java.util.*;
 
public class boj_input {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static StringTokenizer st;
 
    // 4 방향
    static int[] dx = { 10-10 };
    static int[] dy = { 010-1 };
 
    // 8 방향
    static int[] dx2 = { 110-1-1-101 };
    static int[] dy2 = { 01110-1-1-1 };
 
    // gcd (Greatest Common Divisor)
    static int gcd(int a, int b) {
        return a % b == 0 ? b : gcd(a, a % b);
    }
 
    // lcm (Least Common Multiple)
    static int lcm(int a, int b) {
        return a * b / gcd(a, b);
    }
 
    // Sieve of Eratosthenes
    static void sieveOfEratosthenes(boolean[] prime) {
        for (int i = 2; i*< prime.length; i++) {
            for (int j = i*2; j < prime.length; j+=i) {
                prime[j] = false;
            }
        }
    }
    
    // next_Permutation
    static boolean next_Permutation(int[] a, int n) {
        int i = n-1;
        while (i > 0 && a[i-1>= a[i]) i-=1// 1.
        if (i <= 0return false// 마지막 순열
 
        int j = n-1// 2.
        while (a[j] <= a[i-1]) j-=1;
 
        // 3. swap(a[i-1], a[j]);
        int temp = a[i-1];
        a[i-1= a[j];
        a[j] = temp;
 
        j = n-1// 4.
        while (i < j) {
        // swap(a[i], a[j]);
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i += 1; j -= 1;
        }
        return true;
    }
 
    // 2차원 배열 회전
    static int[][] rotate(int turnCount, int[][] sticker) {
        // do turn, turnCount 1 - 90, 2 - 180, 3 - 270 도
        int row = sticker.length;
        int col = sticker[0].length;
        int[][] turnSticker;
 
        // 회전시키기
        if (turnCount == 1) { // 90
            turnSticker = new int[col][row];
            for (int i = 0; i < col; i++) {
                for (int j = 0; j < row; j++) {
                    turnSticker[i][j] = sticker[row-1-j][i];
                }
            }
        } else if (turnCount == 2) { // 180
            turnSticker = new int[row][col];
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    turnSticker[i][j] = sticker[row-1-i][col-1-j];
                }
            }
        } else { // 270
            turnSticker = new int[col][row];
            for (int i = 0; i < col; i++) {
                for (int j = 0; j < row; j++) {
                    turnSticker[i][j] = sticker[j][col-1-i];
                }
            }
        }
        return turnSticker;
    }
}
 
class Pair {
    int x;
    int y;
 
    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
 
 
 
cs

'PS > etc' 카테고리의 다른 글

우테코 3기 코테 후기  (0) 2020.11.07
chapter3-Q3) 배열에서의 검색  (0) 2020.07.13
chapter2) 그 해의 남은 일 수 구하기  (0) 2020.07.09
chapter2) 그 해의 경과 일 수 구하기  (0) 2020.07.09
chapter2) 소수 구하기  (0) 2020.07.09