본문 바로가기
728x90
반응형

알고리즘 문제풀이25

백준 2193 코드 n = int(input()) def dp(n): dp = [0] * (n+1) dp[1] = 1 # 1 if n >= 2: dp[2] = 1 # 10 for i in range(3, n+1): dp[i] = dp[i-1] + dp[i-2] return dp[n] result = dp(n) print(result) 2023. 5. 23.
백준 14888 #2 코드 길이 약간 단축. 백준 14888 다른 풀이 버전. import sys import itertools input = sys.stdin.readline n = int(input()) # 숫자 number = list(map(int, input().split())) #연산자 카운트 operator = list(map(int, input().split())) result = [] real_operator = [] # 연산자로 바꿔주는 함수 def pick_operator(operator): if operator[0] != 0: for i in range(operator[0]): real_operator.append("+") if operator[1] != 0: for i in range(operator[.. 2023. 5. 23.
백준 14888 코드 import sys from itertools import permutations def calculate(operands, operators): result = operands[0] for i in range(1, len(operands)): if operators[i-1] == '+': result += operands[i] elif operators[i-1] == '-': result -= operands[i] elif operators[i-1] == '*': result *= operands[i] elif operators[i-1] == '/': if result < 0: result = -((-result) // operands[i]) else: result //= operands[i] r.. 2023. 5. 23.
백준 2468 *코드 import sys from collections import deque sys.setrecursionlimit(10**6) input = sys.stdin.readline n = int(input()) # 초기 배열 array = [list(map(int, input().split())) for _ in range(n)] # 최대수 maxNumber = max(map(max, array)) # 임시 배열 temp_array = [[0] * n for _ in range(n)] # 물에 잠기는 영역 구분 def area(number): for i in range(n): for j in range(n): if array[i][j] < number: temp_array[i][j] = 1 # 물 잠김 .. 2023. 5. 22.
백준 2667 #2 재귀함수를 사용한 두 번째 풀이. 코드 길이 단축. def dfs(x, y): global count if x = N or y = N or grid[x][y] == 0: return grid[x][y] = 0 # 방문한 집은 0으로 표시 count += 1 dfs(x-1, y) # 상 dfs(x+1, y) # 하 dfs(x, y-1) # 좌 dfs(x, y+1) # 우 N = int(input()) grid = [list(map(int, input().strip())) for _ in range(N)] counts = [] for i in range(N): for j in range(N): if grid[i][j] == 1: count = 0 dfs(i, j) coun.. 2023. 5. 17.
백준 2798 #2 두 번째 풀이 시간은 아니지만 코드 길이는 단축되었다. import itertools a = list(map(int, input().split())) b = list(map(int, input().split())) combinations = itertools.combinations(b, 3) answer=[] for c in combinations: temp = 0 for n in range(3): temp += c[n] answer.append(temp) answer2=[] for n in answer: if n 2023. 5. 17.
728x90
반응형