본문 바로가기

Algorithm(CodeTree, Python)/완전탐색128

[코드트리 자리 마다 숫자를 정하는 완전탐색] 개발팀의 능력 python # ability_list 입력 ability_list = list(map(int, input().split())) # 함수들 # all_diff(a, b, c) def all_diff(a, b, c): # team_1, team_2 team_1, team_2 = ability_list[a] + ability_list[b], ability_list[c] # team_3 team_3 = sum(ability_list) - team_1 - team_2 # 서로 다른지 반환 return team_1 != team_2 and team_2 != team_3 and team_1 != team_3 # calc(a, b, c) def calc(a, b, c): # team_1, team_2 team_1, team_2 .. 2022. 12. 14.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 개발자의 능력 2 python 파이썬 사용자는 참 축복받은 ㅋㅋㅋㅋ max, min 이런거 만들어주셔서 감사합니다. # ability_list 입력 ability_list = list(map(int, input().split())) # 함수들 # calc(a, b, c, d) def calc(a, b, c, d): # sum_1 sum_1 = ability_list[a] + ability_list[b] # sum_2 sum_2 = ability_list[c] + ability_list[d] # sum_3 sum_3 = sum(ability_list) - sum_1 - sum_2 # 반환 return max(sum_1, sum_2, sum_3) - min(sum_1, sum_2, sum_3) # 설계 # min_diff import s.. 2022. 12. 13.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 숫자 카운트 python 숫자야구를 파이썬으로 만들어볼줄이야,, 처음엔 당황했지만, 조건 하나씩 필터링해가며 비교해보면 될 듯 해서 만들어봤더니 돌아갔다. # n 입력 n = int(input()) # conditions conditions = [] # conditions 입력 for _ in range(n): num, cnt_1, cnt_2 = input().split() # 형 변환 cnt_1, cnt_2 = int(cnt_1), int(cnt_2) conditions.append([num, cnt_1, cnt_2]) # 함수들 # cnt_2_is_ok(k, correct_cnt, a, b, c) def cnt_2_is_ok(k, correct_cnt, a, b, c): # curr_cnt curr_cnt = 0 # k 분해.. 2022. 12. 12.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 두 가지로 열리는 자물쇠 python 원형에서 조건 따질때, 절댓값으로 해결하면 편하다는것을 기억하도록 하자는 교훈,,, # n 입력 n = int(input()) # comb_1 입력 comb_1 = list(map(int, input().split())) # comb_2 입력 comb_2 = list(map(int, input().split())) # 함수들 # open_with_comb_1(a, b, c) def open_with_comb_1(a, b, c): # a, b, c와 comb_1[0], comb_1[1], comb_1[2] 각각의 간격이 모두 2 이내이면 통과 return (abs(comb_1[0] - a) = n-2) and \ (abs(comb_1[1] - b) = n-2) and \ (abs(comb_1[2] - c).. 2022. 12. 12.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 개발자의 능력 3 python # ability_list ability_list = list(map(int, input().split())) # 함수들 # calc(a, b, c) def calc(a, b, c): # a, b, c 번째의 합 sum_1 = ability_list[a] + ability_list[b] + ability_list[c] # 이외의 합 sum_2 = sum(ability_list) - sum_1 # 두 값의 차를 반환 return abs(sum_1 - sum_2) # 설계 # min_diff import sys min_diff = sys.maxsize # 완전 탐색 시작 for i in range(4): for j in range(i+1, 5): for k in range(j+1, 6): # min_diff.. 2022. 12. 11.
[코드트리 자리 마다 숫자를 정하는 완전탐색] 한 가지로 열리는 자물쇠python # n 입력 n = int(input()) # password 입력 password = list(map(int, input().split())) # 함수들 # is_open(a, b, c) def is_open(a, b, c): # 하나라도 차가 2 이하면 열림 return abs(a - password[0]) 2022. 12. 11.
[코드트리 구간 단위로 완전탐색] 밭의 높이를 고르게하기 python # n, h, t 입력 n, h, t = map(int, input().split()) # field_list 입력 field_list = list(map(int, input().split())) # 함수들 # calc(s, e) def calc(s, e): # curr_cost curr_cost = 0 # 해당 범위 내에서 for i in range(s, e): # curr_cost에 차이 추가 curr_cost += abs(field_list[i] - h) # 반환 return curr_cost # 설계 # lowest_cost import sys lowest_cost = sys.maxsize # 완전 탐색 시작 for i in range(n-t+1): # lowest_cost 업데이트 lowest_.. 2022. 12. 10.
[코드트리 구간 단위로 완전탐색] G or H 2 python # n 입력 n = int(input()) # linear linear = [0] * 101 # pos, alpha 입력 for _ in range(n): pos, alpha = input().split() # linear에 입력 linear[int(pos)] = alpha # 함수들 # exists(s, e) def exists(s, e): # 양 끝에 사람이 있는지 반환 return linear[s] and linear[e-1] # only_g(s, e) def only_g(s, e): # g_cnt g_cnt = 0 # 현 범위 중 for i in range(s, e): # 한 번이라도 'H'가 나오면 if linear[i] == 'H': # 실패 return False # 다 돌고나서 # g_cn.. 2022. 12. 9.
[코드트리 구간 단위로 완전탐색] 바구니 안의 사탕 2 python 문제에서 말하는게 너무 애매하다. 1. 바구니가 여러 개 들어갈 수 있다는 것이 동일선상의 여러 개 중 가장 큰 하나를 선택해야 한다는 것인지, 하나의 바구니로 합쳐진다는 것인지 알 수 없었다. 2. 앞 뒤 시작점이 모두 범위 내에 들어야 하는지 아닌지 명확하지 않다. # n, k 입력 n, k = map(int, input().split()) # linear linear = [0] * 401 # candy, pos 입력 for _ in range(n): candy, pos = map(int, input().split()) # linear에 추가 linear[pos] += candy # 함수들 # in_range(s, e) def in_range(s, e): # s가 양수이고, e가 400 이하면 통과 .. 2022. 12. 8.
[코드트리 구간 단위로 완전탐색] 특정 구간의 원소 평균값 python # n 입력 n = int(input()) # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # is_ok(a, b) def is_ok(a, b): # curr_sum curr_sum = sum(num_list[a:b]) # curr_avg curr_avg = curr_sum / (b-a) # 현재 범위 내에서 for i in range(a, b): # 평균값이 존재하면, if num_list[i] == curr_avg: # 성공 return True # 다 돌았는데 없으면 실패 return False # 설계 # ans ans = 0 # 완전 탐색 시작 -> 시작점과 끝점을 선택 for i in range(n): for j in range(.. 2022. 12. 7.
[코드트리 구간 단위로 완전탐색] G or H 3 python # n, k 입력 n, k = map(int, input().split()) # linear linear = [ 0 for _ in range(10001) ] # pos, point 입력 for _ in range(n): pos, point = input().split() # linear에 정보추가 linear[int(pos)] = point # 함수들 # calc(j) def calc(j): # curr_sum curr_sum = 0 # 해당 구간의 합 구하기 for i in range(j, j + k + 1): # G이면, if linear[i] == 'G': # 1점 추가 curr_sum += 1 # H이면, elif linear[i] == 'H': # 2점 추가 curr_sum += 2 # 반환 .. 2022. 12. 7.
[코드트리 구간 단위로 완전탐색] 아름다운 수열 2 python # n, m 입력 n, m = map(int, input().split()) # a 입력 a = list(map(int, input().split())) # b 입력 b = list(map(int, input().split())) # 함수들 # is_beautiful(k) def is_beautiful(k): # temp temp = [] # k번째부터 m개를 for i in range(k, k + m): # temp에 저장 temp.append(a[i]) # temp 정렬 temp.sort() # return return temp == b # 설계 # ans ans = 0 # b 정렬 b.sort() # 완전 탐색 시작 for i in range(n - m + 1): # 아름다운 수열이면 if is_b.. 2022. 12. 6.
[코드트리 구간 단위로 완전탐색] 구간 중 최대 합 python # n, k 입력 n, k = map(int, input().split()) # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # calc(j) def calc(j): # curr_sum curr_sum = 0 for i in range(j, j+k): curr_sum += num_list[i] # 반환 return curr_sum # 설계 # max_sum max_sum = 0 # 완전탐색 for i in range(n - k + 1): # max_sum 업데이트 max_sum = max(max_sum, calc(i)) # 출력 print(max_sum) 2022. 12. 5.
[코드트리 자리 수 단위로 완전탐색] 특정 수와 근접한 합 python # n, s 입력 n, s = map(int, input().split()) # num_list 입력 num_list = list(map(int, input().split())) # 함수들 # calc(a, b) def calc(a, b): # curr_sum curr_sum = 0 # num_list의 숫자 중 for i in range(n): # a, b번째 인덱스에 있는 숫자면 if i == a or i == b: # 더하지 않고 통과 continue # 나머지는 curr_sum에 추가 curr_sum += num_list[i] # curr_sum과 s의 차이를 반환 return abs(curr_sum - s) # 설계 # min_diff 설정 import sys min_diff = sys.maxs.. 2022. 12. 4.
[코드트리 자리 수 단위로 완전탐색] 숨은 단어 찾기 2 python # n, m 입력 n, m = map(int, input().split()) # grid 입력 grid = [ input() for _ in range(n) ] # 함수들 # l_to_r(x, y) def l_to_r(x, y): # 범위 내에 있고, 오른쪽 두 개가 'E'면 성공 return y = 2 and grid[x][y-1] == 'E' and grid[x][y-2] == 'E' # u_to_d(x, y) def u_to_d(x, y): # 범위 내에 있고, 아래 두 개가 'E'면 성공 return x = 2 and grid[x-1][y] == 'E' and grid[x-2][y] == 'E' # ru_to_ld(x, y) def ru_to_ld(x, y): # 범위 내에 있고, 왼쪽 대각선 아.. 2022. 12. 3.
[코드트리 자리 수 단위로 완전탐색] Carry 피하기 2 python # n 입력 n = int(input()) # num_list num_list = [] # num_list 입력 for _ in range(n): num_list.append(int(input())) # 함수들 # is_carry(a, b, c) def is_carry(a, b, c): # str_a, b, c str_a = str(num_list[a]) str_b = str(num_list[b]) str_c = str(num_list[c]) # max_len 구하기 max_len = max(len(str_a), len(str_b), len(str_c)) # max_len에 맞춰 형식 변환 ex. "6" -> "0006" len_diff_a = max_len - len(str_a) str_a = '0' .. 2022. 12. 2.
[코드트리 자리 수 단위로 완전탐색] 오목 python # table 입력 table = [ list(map(int, input().split())) for _ in range(19) ] # 함수들 # row_five(x, y) def row_five(x, y): # 범위 내에 없으면 탈락 if y >= 15: return False # flag flag = table[x][y] # 연속 다섯개중 for i in range(1, 5): # 다른 게 있으면, if table[x][y+i] != flag: # 실패 return False # 다 통과하면 성공 return True # col_five(x, y) def col_five(x, y): # 범위 내에 없으면 탈락 if x >= 15: return False # flag flag = table[x][y] .. 2022. 12. 1.
[코드트리 자리 수 단위로 완전탐색] 원 모양으로 되어있는 방 python # n 입력 n = int(input()) # ppl_list ppl_list = [] # ppl_list 입력 for _ in range(n): ppl_list.append(int(input())) # 함수들 # calc(k) def calc(k): # curr_dist curr_dist = 0 # 완전 탐색 시작 for i in range(n): # curr_dist 업데이트 curr_dist += ((i - k + n) % n) * ppl_list[i] # return return curr_dist # 설계 # 최소 거리 import sys min_dist = sys.maxsize # 완전 탐색 시작 for i in range(n): # min_dist 업데이트 min_dist = min(min_d.. 2022. 11. 30.