포도가게의 개발일지
백준 02일차 본문
23. 한수 찾기 한수란? 등차수열로 구성된 수 (홀수자리일때 양쪽합에 /2 == 가운데 수)(짝수 일땐 양쪽의 합치 같아야함)
import sys
count = 0
tmp_arr = []
num = int(sys.stdin.readline())
if num>=100 :
count += 99
check = str(num)
length = len(check)
for x in range(100,num+1):
tmp_arr.append(x)
for y in tmp_arr:
y = str(y)
#print(y, len(y))
if len(y) == 3:
if (int(y[0])+int(y[2]))/2 == int(y[1]):
count+=1
else:
if(int(y[0])+int(y[3])) == (int(y[1])+int(y[2])):
count+=1
print(count)
else:
count += num
print(count)
24. 종이접기 가장 큰 면적 구하기
import sys
x = []
y = []
a,b = list(map(int, sys.stdin.readline().split()))
x.append(a)
y.append(b)
num = int(sys.stdin.readline())
for i in range(num):
select, divide = map(int, sys.stdin.readline().split())
if select == 0:
sum = 0
left = 0
right = 0
length = int(len(y))
for j in range(0,length):
sum += y[j]
#print(sum, y[j])
if sum >= divide:
right = sum - divide
left = y[j] - right
del y[j]
y.insert(j,left)
y.insert(j+1,right)
#print(y)
break
else:
sum = 0
left = 0
right = 0
length = int(len(x))
for j in range(0,length):
sum += x[j]
if sum >= divide:
right = sum - divide
left = x[j] - right
del x[j]
x.insert(j,left)
x.insert(j+1,right)
#print(x)
break
max_x = max(x)
max_y = max(y)
print(max_x*max_y)
25. 재귀함수 팩토리얼
import sys
def my_factorial(n: int):
if(n>1):
return n*my_factorial(n-1)
else:
return 1
num = int(sys.stdin.readline())
print(my_factorial(num))
26. 하노이 탑 재귀함수
import sys
def move(n,a,b):
if n == 0:
return
if n>0:
move(n-1,a,6-a-b)
print(f'{a} {b}', )
if n>0:
move(n-1,6-a-b,b)
num = int(sys.stdin.readline())
print((2**num)-1)
if num <= 20:
move(num,1,3)
27. Z 위치값 찾기 분할정복 재귀함수
import sys
n, r, c = map(int,sys.stdin.readline().split())
count = 0
def divide(n,x,y):
global count
#print(n,x,y,count+1)
if r==x and c==y:
print(count)
if n == 1:
count += 1
return 1
#1
divide(n/2,x,y)
#2
divide(n/2,x,y+n/2)
#3
divide(n/2,x+n/2,y)
#4
divide(n/2,x+n/2,y+n/2)
divide((2**n),0,0)
#####################시간 초과되어 백트레킹 적용##############################
import sys
num, c, r = map(int,sys.stdin.readline().split())
count = 0
sum_x = 2**num/2
sum_y = 2**num/2
def location_check(lens,r,c):
global sum_x, sum_y
if r < sum_x and c < sum_y:
sum_x -= lens/2
sum_y -= lens/2
return 1
elif r >= sum_x and c < sum_y:
sum_x += lens/2
sum_y -= lens/2
return 2
elif r < sum_x and c >= sum_y:
sum_x -= lens/2
sum_y += lens/2
return 3
elif r >= sum_x and c >= sum_y:
sum_x += lens/2
sum_y += lens/2
return 4
def divide(n,x,y,cnt):
if n == 1:
print(int(cnt))
return 1
rectangle_size = n * n / 4
location = location_check(n/2,r,c)
if location == 1:
divide(n/2,x,y,cnt)
elif location == 2:
divide(n/2,x,y+n/2,cnt+rectangle_size)
elif location == 3:
divide(n/2,x+n/2,y,cnt+2*rectangle_size)
elif location == 4:
divide(n/2,x+n/2,y+n/2,cnt+3*rectangle_size)
divide((2**num),0,0 ,0)
########################다른 분이 짠코드 굳이 나처럼 location 반환없이 바로 적용하셨다############
import sys
input = sys.stdin.readline
def recursion(start_row, end_row, start_col, end_col, n):
# 출력 조건
if X == start_row and Y == start_col:
print(n)
return
# 4등분하기 위한 행의 경계값
mid_row = (start_row + end_row) // 2
# 4등분하기 위한 열의 경계값
mid_col = (start_col + end_col) // 2
# 4등분된 사각형의 크기
N = (mid_row - start_row) * (mid_col - start_col)
# 왼쪽 위
if start_row <= X < mid_row and start_col <= Y < mid_col:
recursion(start_row, mid_row, start_col, mid_col, n)
# 오른쪽 위
elif start_row <= X < mid_row and mid_col <= Y < end_col:
recursion(start_row, mid_row, mid_col, end_col, n + N)
# 왼쪽 아래
elif mid_row <= X < end_row and start_col <= Y < mid_col:
recursion(mid_row, end_row, start_col, mid_col, n + 2 * N)
# 오른쪽 아래
elif mid_row <= X < end_row and mid_col <= Y < end_col:
recursion(mid_row, end_row, mid_col, end_col, n + 3 * N)
N, X, Y = map(int, input().split())
recursion(0, 2**N, 0, 2**N, 0)
'백준' 카테고리의 다른 글
python 백준 1655번 수찾기(이분탐색) (0) | 2021.08.15 |
---|---|
python 백준 2468번 안전영역 (0) | 2021.08.11 |
백준 10971번 외판원의 순회 2 (0) | 2021.08.10 |
백준 03일차 (0) | 2021.08.09 |
백준 01일차 (0) | 2021.08.06 |