/images/jg_02.jpg

BEAKJOON 15649, 15650

15649_N과 M (1)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
N, M = map(int, input().split())

def DFS(count):
    if count == M:
        print(*arr)
        return

    for i in range(N):
        if visited[i] == True:
            continue

        visited[i] = True
        arr.append(num_list[i])
        DFS(count+1)
        arr.pop()
        visited[i] = False

num_list = [i + 1 for i in range(N)]
visited = [False] * N
arr = []

DFS(0)

15650_N과 M (2)

 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
import sys

def DFS(count):
    if count == M:
        print(*arr)
        return

    for i in range(N):
        if visited[i] == True:
            continue

        visited[i] = True
        arr.append(num_list[i])
        DFS(count+1)
        arr.pop()

        for j in range(i+1, N):
            visited[j] = False

N, M = map(int, sys.stdin.readline().split())

num_list = [i+1 for i in range(N)]
visited = [False] * N
arr = []

DFS(0)

SW Expert Academy_D4 1258, 1249, 1238, 4261

D4_1258_행렬찾기

 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
def check(x, y):
    dx, dy = 0, 0
    while x + dx < N and arr[x+dx][y]:
        dx += 1
    while y + dy < N and arr[x][y+dy]:
        dy += 1
    result.append([dx*dy, dx, dy])
            
    for i in range(x, x+dx):
        for j in range(y, y+dy):
            arr[i][j] = 0

for T in range(int(input())):
    N = int(input())
    arr = [list(map(int, input().split())) for _ in range(N)]

    result = []
    for i in range(N):
        for j in range(N):
            if arr[i][j] != 0:
                check(i, j)
    
    aws = sorted(result, key=lambda x: [x[0], x[1]])
    print(f'#{T+1} {len(aws)}', *[str(i[1]) + ' ' + str(i[2]) for i in aws])

    
# DFS로 구현해보려 했는데.
# 내가 원하는 위치값을 반환하고 바로 종료시키는 것이
# 불가능해서. 최소한의 값인 가로 세로 이동값만을 뽑아
# 정리하였다.
# 마지막에 정렬이 더 복잡했다..

BEAKJOON 1021, 5430, 10866

1021_회전하는 큐

 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
import sys
from collections import deque

N, M = map(int, sys.stdin.readline().split())
arr = deque([i for i in range(1, N+1)])
poplist = deque(list(map(int, sys.stdin.readline().split())))

count = 0
while poplist:
    try:
        if arr[0] == poplist[0]:
            arr.popleft()
            poplist.popleft()
        L = len(arr)
        a = abs(arr.index(arr[0]) - arr.index(poplist[0]))
        b = L - a
        if a < b:
            arr.rotate(-a)
            count += a
        elif a >= b:
            arr.rotate(b)
            count += b
    except IndexError:
        break

print(count)

5430_AC

 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
import sys
from collections import deque

for T in range(int(sys.stdin.readline().rstrip())):
    P = sys.stdin.readline().rstrip()
    N = int(sys.stdin.readline().rstrip())
    ## 다양하게 input 받는 거 고민하기 좋다.
    arr = deque(list(sys.stdin.readline().rstrip()[1:-1].split(",")))

    if N == 0 or len(arr) == 0:
        arr = deque([])

    reverse = False
    error = False
    for i in P:
        if i == "R":
            reverse =  not reverse
        elif i == "D":
            if len(arr) == 0:
                error = True
                break
            if reverse:
                arr.pop()
            else:
                arr.popleft()
    if error:
        print("error")
    else:
        if reverse:
            arr.reverse()
        print("[", end="") 
        print(",".join(arr), end="") 
        print("]")


    ## reverse() 여러번 돌리면 시간초과 뜬다. 
    # flag = True
    # for i in P:
    #     if i == "R":
    #         arr.reverse()
    #     else:
    #         if len(arr) != 0:
    #             arr.popleft()
    #         else:
    #             print("error")
    #             flag = False
    #             break
    
    # if flag:
    #     if len(arr) != 0:

    #         print("[", end="") 
    #         print(",".join(arr), end="") 
    #         print("]")
    #     else:
    #         print('error')

BEAKJOON 18258, 2164, 11866, 1966

18258_큐 2

 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
import sys
from collections import deque
# import time

N = int(sys.stdin.readline().rstrip())
# start = time.time()  # 시작 시간 저장

q = deque()

for _ in range(N):
    command = sys.stdin.readline().split() 
    if command[0] == "push":
        q.append(command[1])
    elif command[0] == "pop":
        if q:
            print(q.popleft())
        else:
            print(-1)
    elif command[0] == "size":
        print(len(q))
    elif command[0] == "empty":
        if not q:
            print(1)
        else:
            print(0)
    elif command[0] == "front":
        if q:
            print(q[0])
        else:
            print(-1)
    elif command[0] == "back":
        if q:
            print(q[len(q)-1])
        else:
            print(-1)

# print("time :", time.time() - start)  # 현재시각 - 시작시간 = 실행 시간


# q[len(q)-1] VS q[-1] 속도 비교
# q[len(q)-1] 이 더 빠름.

BEAKJOON 4949, 10828, 1874, 17298

4949_균형잡힌 세상

 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
import sys

while True:
    text = sys.stdin.readline().rstrip()
    if text == ".":
        break
    stack =[]
    result = "yes"
    for i in text:
        if i.isalpha() or i == " ":
            continue
        elif i == "(" or i == "[":
            stack.append(i)
        elif i == ")":
            if not stack or stack[-1] != "(":
                result = "no"
                break
            else:
                stack.pop()
        elif i == "]":
            if not stack or stack[-1] != "[":
                result = "no"
                break
            else:
                stack.pop()

    if not stack:
        print(result)
    else:
        print('no')

BEAKJOON 1037, 1934, 2609, 5086

1037_약수

1
2
3
4
5
6
7
8
import sys

N = int(sys.stdin.readline().rstrip())
arr = list(map(int, sys.stdin.readline().split()))

Max = max(arr)
Min = min(arr)
print(Max*Min)

1934_최소공배수

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import sys

def GCD(a, b):
    while b != 0:
        m = a % b
        a = b
        b = m
    return a

for T in range(int(sys.stdin.readline().rstrip())):
    A, B = map(int, sys.stdin.readline().split())

    gcd = GCD(A, B)
    a = A // gcd
    b = B // gcd
    print(a*b*gcd)

2609_최대공약수와 최소공배수

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import sys

def GCD(a, b):  #최대공약수(유클리드 호제법)
    while b != 0:
        m = a % b
        a = b
        b = m
    return a
    

A, B = map(int, sys.stdin.readline().split())

gcd = GCD(A, B)

a = A // gcd
b = B // gcd            

print(gcd)
print(a*b*gcd)