/images/jg_02.jpg

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)

BEAKJOON 1931, 11399, 1541, 13305

1931_회의실 배정

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

N = int(sys.stdin.readline().rstrip())

arr = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
schedule = sorted(arr, key=lambda x: (x[1], x[0]))

count, end = 0, 0
for s, f in schedule:
    if s >= end:
        count += 1
        end = f

print(count)

# 왠지 정렬을 잘하면 계산이 쉬울 것 같아서. 
# 시작시간과 종료시간을 정렬해 계산했는데
# 1. 종료시간, 2. 시작시간 순위로 정렬해야 했다.

SW Expert Academy_D4 1226, 1227, 1231, 5643

D4_1226_미로1

 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
dx = [0,1, 0, -1]
dy = [1,0, -1, 0 ]

def IsSafe(x, y):
    if 0<= x < 16 and 0 <= y < 16:
        return True

def DFS(x, y):
    global result
    visited[x][y] = 1
    if arr[x][y] == 3:
        result = 1
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        if IsSafe(nx, ny) and visited[nx][ny] == 0 and arr[nx][ny] != 1:
            DFS(nx, ny)
            

def Find_Start():
    for i in range(16):
        for j in range(16):
            if arr[i][j] == 2:
                return i, j


for T in range(10):
    t = input()
    arr = [list(map(int, input())) for _ in range(16)]
    visited = [[0 for _ in range(16)] for _ in range(16)]

    x, y = Find_Start()
    result = 0
    DFS(x, y)
    
    print(f'#{T+1} {result}')


# 재귀를 사용할 시 return은 불편한 점이 많다.
# 하여 특정 조건에서의 값이 필요한 경우.
# 변수를 설정해서 그 값을 바꾸는 편이 간편하다. 
# 다만 런타임에 조심해야 한다.

SW Expert Academy_D4 1218, 1219, 1222, 1223

D4_1218_괄호 짝짓기

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def Pair_bracket():
    for i in range(L):
        if q[i] in check:
            ni = i - 1
            while True:
                if q[ni] == 0:
                    ni -= 1
                else:
                    break
            a = q[ni]
            b = check[q[i]]
            if a == b:
                q[i] = 0
                q[ni] = 0
            else:
                return 0
    return 1

check = {")" :"(", "]":"[", "}":"{", ">":"<"}
for T in range(10):
    L = int(input())
    q = list(input())

    print(f'#{T+1} {Pair_bracket()}')

D4_1219_길찾기

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

def BFS():
    visited[0] = 1
    while q:
        a = q.popleft()
        if a == 99:
            return 1
        if visited[a] == 0:
            for i in range(len(arr[a])):
                q.append(arr[a][i])
            visited[a] = 1
    return 0   

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

    for i in range(0, N*2, 2):
        arr[route[i]].append(route[i+1])
        
    q = deque()
    for i in range(len(arr[0])):
        q.append(arr[0][i])

    print(f'#{T} {BFS()}')

# 빈 2차원 리스트 만드는 법
# [[] * 100]  은 안됨
# [[] for _ in range(N)] 으로 생성