오늘의 백준 문제
우선, 예제를 직접 따라하면서 문제를 이해하고 대략적인 알고리즘을 구상한다. 반드시 input size를 고려해서 최대 complexity를 계산한다.
brute_force 형태의 구현 문제이고 len(li)가 1000을 넘지 않는다. 입력에 따라 처리할 데이터가 급감함으로 실행 시간은 넉넉할거라 판단했다.
import sys
input = sys.stdin.readline
N = int(input())
def strike_ball(x,y):
x = str(x)
y = str(y)
s=0
b=0
for i in range(3):
if x[i] == y[i]:
s +=1
elif x[i] in y:
b +=1
return s,b
def dup(x):
x = str(x)
if(x[0] == x[1]):
return True
elif(x[0] == x[2]):
return True
elif(x[1] == x[2]):
return True
return False
old = []
for i in range(100,1000):
if(dup(i) or '0' in str(i)):
continue
old.append(i)
#print(i)
new = []
for h in range(N):
com = list(map(int,input().split()))
num = com[0]
strike = com[1]
ball =com[2]
for number in old:
if(strike_ball(num,number) == (strike,ball)):
#print(strike_ball(num,number) , strike,ball)
new.append(number)
old = new
new = []
#print(old)
print(len(old))
우수 코드
from itertools import permutations
def determine(q):
suspect_li = q[0]
q_s = int(q[1])
q_b = int(q[2])
ball = {0: (1, 2), 1: (2, 0), 2: (1, 0)}
good_li = []
for ele in num_li:
s, b = 0, 0
for i in range(3):
if suspect_li[i] == ele[i]:
s += 1
for i in range(3):
for j in range(2):
if suspect_li[i] == ele[ball[i][j]]:
b += 1
if (q_s, q_b) == (s, b):
good_li.append(ele)
return good_li
# creating possible array
li = permutations([i+1 for i in range(9)], 3)
num_li = []
for ele in li:
num_li.append(''.join(map(str, ele)))
q_n = int(input())
for _ in range(q_n):
question = input().split()
num_li = determine(question)
print(len(num_li))
내 코드
중간에 코드가 내 생각대로 흘러가고 있는지 확인하기 위해서 print()를 썼다, 지웠다 했지만 주석 처리를 함으로써 필요할 때, 다시 쓰고 실제 실행에서는 넘어갈 수 있도록 하였다.
import sys
input = sys.stdin.readline
# input
n, m = map(int, input().split())
h_n = int(input())
h_li = []
for _ in range(h_n):
h_li.append(list(map(int, input().split())))
# print(h_li)
guard_h = list(map(int, input().split()))
result = 0
for i in h_li:
# x is guard's line, y is another one
x, y = guard_h[0], i[0]
a, b = guard_h[1], i[1]
if x == y:
result += abs(a - b)
else:
if x*y == 2:
result += min(2*n-a-b+m, a+b+m)
elif x*y == 12:
result += min(2 * m - a - b+n, a + b+n)
elif x*y == 3:
result += a+b
elif x*y == 8:
result += n+m-a-b
elif x*y == 4:
if x == 1:
result += b+n-a
else:
result += n-b+a
elif x*y == 6:
if x == 2:
result += a+m-b
else:
result += m-a+b
print(result)
def get_address(i, j):
if i == 1:
address = j
elif i == 2:
address = C+R+C-j
elif i == 3:
address = C+R+C+R-j
elif i == 4:
address = C+j
return address
C, R = map(int, input().split())
N = int(input())
stores = [list(map(int, input().split())) for _ in range(N)]
DK_i, DK_j = list(map(int, input().split()))
DK_address = get_address(DK_i, DK_j)
address = []
for i, j in stores:
address.append(get_address(i, j))
circumference = 2 * (R + C)
diff = []
for elem in address:
dist = abs(DK_address - elem)
dist_2 = circumference - dist
shorter = min(dist, dist_2)
diff.append(shorter)
print(sum(diff))
잘못된 아이디어를 기반으로 알고리즘을 짜면 이런 식으로 고생할 수 있다.
곧 있을 적성 시험을 대비해서 재미삼아 사고력 문제 연습을 하였다.
예전에 시청했던 프로그램 중에 더 지니어스; 파이널, 미스터리 사인에서 좋은 예제가 있기에 풀어봤다.
가운데 있는 연산을 유추해서 제시한 문제의 답을 찾는 것이다.
16 ㅁ 9 = ?
36 ㅁ 87 = 12351
4 ㅁ 19 = ?
51 ㅁ 5 = 1
1 ㅁ 51 = 0
21 ㅁ 15 = ?
87 ㅁ 23 = 75
14 ㅁ 81 = 45
1 ㅁ 197652 = 30
11 ㅁ 14 = ?
79 ㅁ 897687543217 = 749282615141312111
897687543217 ㅁ 89 = 839273615141312111
897687543217 ㅁ 897687543217 = 849276625242322212
99 ㅁ 897687543217 = 938273615141312111
17 ㅁ 23 = ?
49 ㅁ 89 = 30
18 ㅁ 13 = ?
19 ㅁ 85 = 8
72 ㅁ 18 = 6
18 ㅁ 77 = 11
18 ㅁ 14 = 8
8 ㅁ 10 = ?
26 ㅁ 51 = 1
74 ㅁ 51 = 0
51 ㅁ 52 = 0
51 ㅁ 10 = 1
1 ㅁ 51 = 0
51 ㅁ 8 = 2
16 ㅁ 24 = ?
16 ㅁ 62 = 2
16 ㅁ 25 = 1
16 ㅁ 23 = 1
16 ㅁ 26 = 2
66 ㅁ 16 = 2
27 ㅁ 2 = ?
27 ㅁ 3 = 7
27 ㅁ 4 = 7
27 ㅁ 5 = 6
27 ㅁ 6 = 6
27 ㅁ 7 = 5
정답
? = 37
큰 수 + (두 수의 차)
? = 257
(두 수의 합, 두 수의 차)
? = 3
나머지
? = 18
(자리수의 합) 곱하기
? = 1341
왼쪽에서부터 나오는 순서대로 자리수의 개수
? = 13
자리수의 합계
? = 7
시간으로 계산했을 때의 합
? = 3
동그라미의 개수
? = 8
최대공약수
? = 8
두 수 사이의 소수 개수
추가로 가장 약한 국어 유형을 대비하기 위해서(나오는 지는 모르나 다른 부분이 자신있기에!) 작년 수능 문제를 풀어보았다.
결과는 참담했고 비문학을 너무 멀리 한 것이 아닌가 했다. 결국 자신 있는 분야에서 점수를 따고 시험 전날에 다시 한번 풀어보기로 했다.