Algorithm/BOJ20 [BOJ] 12871 | 무한 문자열 12871 | 무한 문자열 https://www.acmicpc.net/problem/12871 작성 코드 def gcd(a: int, b: int) -> int : while b != 0: a, b = b, a % b return a def lcm(a: int, b: int) -> int : return a * b // gcd(a, b) a = input() b = input() # 최소 공배수 lcm = lcm(len(a), len(b)) if a*(lcm//len(a)) == b*(lcm//len(b)): print(1) else: print(0) 이상 코드 a, b = input(), input() print(int(a*len(b) == b*len(a))) 문자열 * 숫자 = 문자열을 숫자만큼 반복.. 2022. 1. 27. [BOJ][#] 1002 | 터렛 1002 | 터렛 https://www.acmicpc.net/problem/1002 이상 코드 import math n = int(input()) for _ in range(n): x1, y1, r1, x2, y2, r2 = map(int, input().split()) distance = math.sqrt((x1-x2)**2 + (y1-y2)**2) # The distance of two circles if distance == 0 and r1 == r2 : # two circles are concentric and have the same radius print(-1) elif abs(r1-r2) == distance or r1 + r2 == distance: # internal or external.. 2022. 1. 25. [BOJ][#] 1920 | 수 찾기 1920 | 수 찾기 https://www.acmicpc.net/problem/1920 작성 코드 import sys N = int(sys.stdin.readline()) nums = list(map(int, sys.stdin.readline().split())) M = int(sys.stdin.readline()) M_nums = list(map(int, sys.stdin.readline().split())) for M_num in M_nums: if M_num in nums: print('1') else: print('0') 시간 초과 이상 코드 set 사용 import sys N = int(sys.stdin.readline()) nums = set(map(int, sys.stdin.readline(.. 2022. 1. 24. [BOJ] 1918 | 후위 표기식 1918 | 후위 표기식 https://www.acmicpc.net/problem/1918 작성 코드 express = input() stack = [] answer = [] for exp in express: if exp not in ['+', '-', '*', '/', '(', ')']: answer.append(exp) elif exp == '(': stack.append(exp) elif exp == ')': while stack[-1] != '(': answer += stack.pop() stack.pop() elif exp in ['+', '-']: while stack and (stack[-1] in ['*', '/', '-', '+']): answer += stack.pop() stack... 2022. 1. 21. [BOJ] 2846 | 오르막길 2846 | 오르막길 https://www.acmicpc.net/problem/2846 작성 코드 N = int(input()) nums = list(map(int, input().split())) temp = [] heights = [] for num in nums: if temp and (num nums[i-1]: diff += nums[i] - nums[i-1] else: diff = 0 if diff > heights: heights = diff print(heights) temp 필요 없음, 그냥 바로 전 값이랑 계산 heigts 내 max 값 계산 없이 비교하여 바꿔줌 학습 max 값 단순 비교만 한다면 변수에 임시 max 값 지정해주고 갱신하는 식으로 전 값과 비교해야 한다면 index 번호.. 2022. 1. 19. [BOJ] 1929 | 소수 구하기 1929 | 소수 구하기 https://www.acmicpc.net/problem/1929 작성 코드 import math N, M = map(int, input().split()) for n in range(N, M+1): dec = True if n == 1: continue for i in range(2, int(math.sqrt(n))+1): if n%i == 0: dec = False break if dec: print(n) 범위를 제곱근으로 지정함으로써 시간 초과 해결 이상 코드 에라토스테네스의 체 m, n = map(int, input().split()) n += 1 prime = [True] * n for i in range(2, int(n**0.5)+1): if prime[i]: for .. 2022. 1. 13. 이전 1 2 3 4 다음 반응형