본문 바로가기

Algorithm(CodeTree, Python)127

[코드트리 기준을 새로 설정하여 완전탐색] 독서실의 거리두기 5 Python # n 입력 n = int(input()) # study_cafe 입력 study_cafe = input() # 함수들 # get_max_dist(curr_study_cafe) def get_max_dist(curr_study_cafe): # people_pos people_pos = [] # curr_study_cafe 돌면서 for i in range(n): # 사람이 있으면 if curr_study_cafe[i]: # people_pos에 기록 people_pos.append(i) # people_num -> 현재 독서실에 있는 사람의 수 people_num = len(people_pos) # curr_min_dist curr_min_dist = sys.maxsize for i in range(pe.. 2022. 12. 31.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 원소 값들의 최대 합 Python # n, m 입력 n, m = map(int, input().split()) # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # calc(start_idx) def calc(start_idx): # temp_list temp_list = num_list[:] # curr_idx, curr_sum curr_idx, curr_sum = start_idx, 0 # m 번의 움직임 for _ in range(m): # 이동할 숫자 curr_sum에 담아줌 curr_sum += temp_list[curr_idx] # curr_idx 바꿔줌 curr_idx = temp_list[curr_idx] - 1 # 반환 return curr_sum # 설계 #.. 2022. 12. 30.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 등장하지 않는 문자열의 길이 Python # n 입력 n = int(input()) # string 입력 string = input() # 함수들 # exists_more_than_two_times(search_str) def exists_more_than_two_times(search_str): # 검색하는 문자열 중 for s in search_str: # curr_cnt curr_cnt = 0 # 같은 문자열 내에서 for another_s in search_str: # 자신과 같은 문자열을 발견하면 if s == another_s: # curr_cnt 올려주기 curr_cnt += 1 # 한번이라도 두 번 이상 있었으면 if curr_cnt >= 2: # 성공 return True # 없었으면 실패 return False # get_cu.. 2022. 12. 29.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 좌표평면 위의 균형 2 Python # n 입력 n = int(input()) # point_pos point_pos = list() # x, y 입력 for _ in range(n): point_pos.append(tuple(map(int, input().split()))) # 함수들 # get_q1(x, y) def get_q1(x, y): # point_cnt point_cnt = 0 # point_pos를 돌면서 for point in point_pos: # unpacking p_x, p_y = point # 1사분면에 위치해 있으면 if p_x > x and p_y > y: # point_cnt 올려주기 point_cnt += 1 # 반환 return point_cnt # get_q2(x, y) def get_q2(x, y): #.. 2022. 12. 29.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 팀으로 하는 틱택토 2 Python # table 입력 table = [ list(input()) for _ in range(3) ] # search_row(a, b) def search_row(a, b): # 모든 행을 조사 for i in range(3): # 한번이라도 성공하면 if (table[i][0] == a or table[i][0] == b) and \ (table[i][1] == a or table[i][1] == b) and \ (table[i][2] == a or table[i][2] == b) and \ not (table[i][0] == table[i][1] and table[i][1] == table[i][2]): # 성공 return True # 이외에는 실패 return False # search_col(a, .. 2022. 12. 28.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 수를 여러번 사용하여 특정 수 만들기 Python a, b, c = map(int, input().split()) def get_max_num(m, M): curr_max = 0 while True: if m * curr_max > M: return curr_max else: curr_max += 1 def calc(first, second): curr_sum = a * first + b * second if curr_sum > c: return 0 else: return curr_sum max_len = get_max_num(a, c) max_num = 0 for i in range(max_len): for j in range(max_len): max_num = max(max_num, calc(i,j)) print(max_num) 2022. 12. 27.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 숫자 2배 후 하나 제거하기 Python # n 입력 n = int(input()) # num_list num_list = list(map(int, input().split())) # 함수들 # calc_2(doubled_nums, k) def calc_2(doubled_nums, k): # temp_nums temp_nums = [] for i in range(n): # 제외할 인덱스일 경우 if i == k: # skip continue # 이외에는 else: # temp_nums에 추가 temp_nums.append(doubled_nums[i]) # curr_diff curr_diff = 0 for i in range(n-2): curr_diff += abs(temp_nums[i] - temp_nums[i+1]) # 반환 return cu.. 2022. 12. 27.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 3개의 선 2 Python # n 입력 n = int(input()) # points points = list() # x, y 입력 for _ in range(n): points.append(tuple(map(int, input().split()))) # 함수들 # vertical(k) def vertical(k): # curr_line curr_line = [] # points를 돌면서 for x, y in points: # x좌표가 현재 수직 선분 내에 있는 점이면, if x == k: # curr_line에 추가 curr_line.append((x, y)) # curr_line 반환 return curr_line # parallel(k) def parallel(k): # curr_line curr_line = [] # poi.. 2022. 12. 26.
[코드트리 상황을 일일이 가정해보고 진행하는 완전탐색] 야바위 Python # n 입력 n = int(input()) # order_list order_list = [] # a, b, c 입력 for _ in range(n): order_list.append(tuple(map(int, input().split()))) # 함수들 # simulate(k) def simulate(k): # curr_cups curr_cups = [0] * 3 # 조약돌 넣기 curr_cups[k] = 1 # curr_point curr_point = 0 # simulation for i in range(n): # unpacking a, b, c = order_list[i] a, b, c = a-1, b-1, c-1 # temp_1, temp_2 temp_1, temp_2 = curr_cups[a].. 2022. 12. 26.
[코드트리 값을 기준으로 완전탐색] 팰린드롬 수 찾기 Python # x, y 입력 x, y = map(int, input().split()) # 함수들 # is_palindrome(k) def is_palindrome(k): # str_k str_k = str(k) # len_k len_k = len(str_k) # 맨 앞 인덱스와 맨 뒤인덱스를 기준으로 비교해가며 for i in range(len_k): # 서로 맞지 않는 부분이 있으면 if str_k[i] != str_k[-(i+1)]: # 실패 return False # 다 통과하면 성공 return True # 설계 # palindrome_cnt palindrome_cnt = 0 # 완전 탐색 시작 for i in range(x, y+1): # 팰린드롬수이면 if is_palindrome(i): # palin.. 2022. 12. 25.
[코드트리 값을 기준으로 완전탐색] 정보에 따른 숫자 2 Python # t, a, b 입력 t, a, b = map(int, input().split()) # linear linear = [0] * 1001 # c, x 입력 for _ in range(t): c, x = input().split() linear[int(x)] = c # 함수들 # get_most_close_s(idx) def get_most_close_s(idx): # s_idx_list s_idx_list = list() # linear 탐색 for i in range(1001): # S를 찾으면, if linear[i] == 'S': # 인덱스를 기록 s_idx_list.append(i) # min_dist min_dist = sys.maxsize # 각 인덱스와의 차이를 구해가며 for s_idx .. 2022. 12. 24.
[코드트리 값을 기준으로 완전탐색] 빙산의 일각 2 Python 이어진 빙산을 판단할 때, 맨 왼쪽을 예외처리해주는 생각을 떠올리지 못하는 바보같은 짓을 했다. # n 입력 n = int(input()) # ice_berg ice_berg = list() # h(i) 입력 for _ in range(n): ice_berg.append(int(input())) # 함수들 # simulate(curr_height) def simulate(curr_height): # curr_cnt curr_cnt = 0 # 맨 왼쪽은 떠있으면 if ice_berg[0] > curr_height: # curr_cnt에 추가 curr_cnt += 1 # 이후의 ice_berg를 돌면서 for i in range(1, n): # 전 인덱스가 잠겼고, 자기는 떠있으면 if ice_berg[i-.. 2022. 12. 24.
[코드트리 값을 기준으로 완전탐색] 흥미로운 숫자 2 Python # x, y 입력 x, y = map(int, input().split()) # 함수들 # one_is_once(temp_list) def one_is_once(temp_list): # 1이 존재하는지 반환 return 1 in temp_list # two_types_of_num(temp_list) def two_types_of_num(temp_list): # used_num used_num = 0 # 사용된 숫자를 조사 for num in temp_list: # 사용되었으면 if num: # used_num 올려주기 used_num += 1 # 두 가지 종류를 사용했는지 반환 return used_num == 2 # is_interesting(k) def is_interesting(k): # 0 ~ 9.. 2022. 12. 23.
[코드트리 값을 기준으로 완전탐색] 등차수열 Python # n 입력 n = int(input()) # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # makes_ap(num_1, k, num_2) def makes_ap(num_1, k, num_2): # num_1 - k 가 k - num_2가 같은지 반환 return num_1 - k == k - num_2 # simulate(k) def simulate(k): # curr_cnt curr_cnt = 0 # 완전 탐색 시작 for i in range(n-1): for j in range(i+1, n): # 두 수 사이에 k를 넣었을 때 # 등차수열을 만족하면 if makes_ap(num_list[i], k, num_list[j]): # cur.. 2022. 12. 23.
[코드트리 값을 기준으로 완전탐색] 데이터센터의 온도 조정 2 Python # n, c, g, h 입력 n, c, g, h = map(int, input().split()) # preference_list preference_list = list() # preference_list 입력 for _ in range(n): preference_list.append(tuple(map(int, input().split()))) # 함수들 # calc(celcious, ta, tb) def calc(celcious, ta, tb): # 현재 온도가 ta보다 작다면 if celcious < ta: # c 반환 return c # 현재 온도가 ta이상 tb 이하라면 elif ta 2022. 12. 22.
[코드트리 값을 기준으로 완전탐색] 숫자들의 합 중 최대 Python # x, y 입력 x, y = map(int, input().split()) # 함수들 # calc(k) def calc(k): # curr_sum curr_sum = 0 # str_k str_k = str(k) for i in str_k: curr_sum += int(i) # 반환 return curr_sum # 설계 max_sum = 0 # 완전 탐색 시작 for i in range(x, y+1): # max_sum 업데이트 max_sum = max(max_sum, calc(i)) # 출력 print(max_sum) 2022. 12. 22.
[코드트리 물체 단위로 완전탐색] 스승의 은혜 3 Python # n, b 입력 n, b = map(int, input().split()) # price_list price_list = list() # price_list 채우기 for _ in range(n): price_list.append(tuple(map(int, input().split()))) # 함수들 # discount(k) def discount(k): # unpacking p1, p2 = price_list[k] # 반값으로 내려 반환 return p1//2 + p2 # buy_max(curr_list) def buy_max(curr_list): # curr_list 정렬 curr_list.sort() # 최대 구입 갯수 구하기 for i in range(1, n): # 예산을 넘으면 if sum(.. 2022. 12. 21.
[코드트리 물체 단위로 완전탐색] 상해버린 치즈 Python 코드 짜는거는 안어려웠는데, 문제 자체를 이해하는게 어려웠다. # N, M, D, S 입력 N, M, D, S = map(int, input().split()) # time_table time_table = [] # time_table 채우기 for _ in range(D): time_table.append(tuple(map(int, input().split()))) # sick_time sick_time = [] # sick_time 채우기 for _ in range(S): sick_time.append(tuple(map(int, input().split()))) # 함수들 # find_eaten_cheese(person, time) def find_eaten_cheese(person, time): #.. 2022. 12. 20.