본문 바로가기

전체 글

(209)
수열의 합[백준 1024] https://www.acmicpc.net/problem/1024  수열 합 공식으로 조건에 부합하는 수열을 찾아본다. #include #include // abs() 함수 사용을 위해 추가int main() { int N, L; scanf("%d %d", &N, &L); int result[101]; // 최대 100개의 연속된 숫자를 저장할 수 있는 배열 int temp = 0; int length = 0; // 실제 결과 배열의 길이 while(1) { int start = (2*N - L*L + L) / (2*L); // 등차수열의 합 공식을 사용하여 첫 항 계산 int end = start + L - 1; if..
대지[백준 9063] https://www.acmicpc.net/problem/9063 점이 몇 개를 입력 받는 지와 상관없이 x,y좌표의 최소 최대값만 알면 풀 수 있는 문제이다.  n=int(input())minx,miny,maxx,maxy =0,0,0,0xarr,yarr=[],[]for _ in range(n): a,b = map(int, input().split()) xarr.append(a) yarr.append(b) xarr.sort()yarr.sort()print((xarr[-1]-xarr[0])*(yarr[-1]-yarr[0]))
팰린드롬 만들기[백준 1213] https://www.acmicpc.net/problem/1213 from collections import Counter# 입력 문자열을 리스트로 변환string = list(input().rstrip())# 문자열 정렬string.sort()# 문자별 개수를 세기counter = Counter(string)# 팰린드롬 생성을 위한 준비front_part = [] # 팰린드롬의 앞부분middle_part = [] # 팰린드롬 중앙의 문자 (홀수 개인 문자)for char, count in counter.items(): if count % 2 == 1: middle_part.append(char) front_part.extend([char] * (count // 2))# 팰린드..
부분합[백준 1806] https://www.acmicpc.net/problem/1806 배열 첫 번째 숫자부터 계속 오른쪽으로 window를 늘려가지만, 목표 합 s보다 큰 경우는 window 사이즈를 줄이기 위해 앞 부분인 start를 한 칸 이동한다.  def min_subarray_length(n, s, arr): start, end = 0, 0 current_sum = 0 min_length = float('inf') while end = s: min_length = min(min_length, end - start) current_sum -= arr[start] start += 1 return min_length if..
경고[백준 3029] https://www.acmicpc.net/problem/3029 입력 받은 시간들의 차이가 어느 정도 인지만 구하면 되기 때문에 시, 분, 초를 모두 더하고 서로 빼면 된다.  // 3029 경고// https://www.acmicpc.net/problem/3029#include int main(){ int Shour, Sminute, Ssecond; int Ehour, Eminute, Esecond; int time_sum = 0, Ssum = 0, Esum = 0; scanf("%d:%d:%d", &Shour, &Sminute, &Ssecond); scanf("%d:%d:%d", &Ehour, &Eminute, &Esecond); Ssum = Shour * 3600 ..
토너먼트[백준 1057] https://www.acmicpc.net/problem/1057 라운드가 올라갈 수록 숫자를 최신화 해주면 된다. import sys# 입력 받기n, kim, im = map(int, input().split())# 라운드 수 초기화round = 0while kim != im: # 라운드마다 번호 업데이트 kim = (kim + 1) // 2 im = (im + 1) // 2 # 라운드 카운트 증가 round += 1# 결과 출력print(round)
통나무 건너뛰기[백준 11497] https://www.acmicpc.net/problem/11497  맨 첫 번째 통나무와 마지막 통나무도 인접해 있다고 봐야 하기 때문에 무조건 통나무 높이에 따른 정렬을 하면 안 된다. 2칸씩 떨어진 통나무들의 높이를 비교해야 한다. t =int(input())for _ in range(t): n = int(input()) arr =list(map(int, input().split())) arr.sort() ans = 0 for i in range(2,n): ans=max(ans,arr[i]-arr[i-2]) print(ans)
사과 빨리 먹기[백준 26170] https://www.acmicpc.net/problem/26170 dfs와 백트래킹을 사용하는 함수 find는 이상이 없으나, 처음 시작 지점부터 사과를 얻는 경우에 대한 처리, 시작지점을 -1로 바꿔주는 처리가 있어야 답이 된다. arr = []for i in range(5): oneline = list(map(int, input().split())) arr.append(oneline)x, y = map(int, input().split())ans = []dx = [1, -1, 0, 0]dy = [0, 0, 1, -1]def find(x, y, ate, walked, ans): if ate == 3: ans.append(walked) return for ..