알고리즘

알고리즘

[백준] 1436번(python 파이썬)

n = int(input()) cnt = 0 idx = 666 while True: if '666' in str(idx): cnt += 1 if cnt == n: print(idx) break idx += 1 처음 접근 했을 때 666 앞에 n - 1만큼 붙이면 되겠다고 생각 함 오류.. 7. 6660 8. 6661 9. 6662 .... 위처럼 뒤에 6 아닌 수 가능함 666 부터 시작해서 cnt == n 일때까지 반복

알고리즘

<백준> 1018번 자바 알고리즘

import java.util.*; public class Main { public static boolean[][] arr; public static int min = 64; public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int M = in.nextInt(); arr = new boolean[N][M]; // 배열 입력 for (int i = 0; i < N; i++) { String str = in.next(); for (int j = 0; j < M; j++) { if (str.charAt(j) == 'W') { arr[i][j] = true;// W일 때는 true ..

알고리즘

<백준> 7568번 파이썬 알고리즘

n = int(input()) arr_w = [] arr_h = [] for _ in range(n): w, h = map(int, input().split()) arr_w.append(w) arr_h.append(h) rank = [] for i in range(n): r = 1 for j in range(n): if i == j: continue if arr_w[i] < arr_w[j] and arr_h[i] < arr_h[j]: r += 1 rank.append(r) for i in rank: print(i, end=" ") 어렵게 생각 할 필요 없이 자신의 등수 = 자신보다 키, 몸무게 큰 사람 + 1

알고리즘

<백준> 17478번 파이썬 알고리즘

def recursion(m): print("_" * (4 * (n - m)) + '"재귀함수가 뭔가요?"') if m == 0: print("_" * (4 * (n - m)) + '"재귀함수는 자기 자신을 호출하는 함수라네"') print("_" * (4 * (n - m)) + '라고 답변하였지.') return print("_" * (4 * (n - m)) + '"잘 들어보게. 옛날옛날 한 산 꼭대기에 이세상 모든 지식을 통달한 선인이 있었어.') print("_" * (4 * (n - m)) + '마을 사람들은 모두 그 선인에게 수많은 질문을 했고, 모두 지혜롭게 대답해 주었지.') print("_" * (4 * (n - m)) + '그의 답은 대부분 옳았다고 하네. 그런데 어느 날, 그 선인에게 ..

알고리즘

<백준> 1644번 파이썬 알고리즘

N = int(input()) #에라토스테네스의 체 arr = [False, False] + [True] * (N - 1) prime = [] for i in range(2, N + 1): if arr[i] == True: prime.append(i) for j in range(i+i, N + 1, i): arr[j] = False l = 0 r = 1 res = 0 while r

알고리즘

<백준> 1806번 파이썬 알고리즘 [투 포인터][부분 합]

N, S = map(int, input().split()) li = list(map(int, input().split())) s = 0 e = 0 summary = 0 prefix_sum = [0] for i in li: summary += i prefix_sum.append(summary) ans = 1000001 s = 0 e = 1 while s != N: ## S 이상 일 때 if prefix_sum[e] - prefix_sum[s] >= S: ## 길이 최소 갱신하기 if e - s < ans: ans = e - s ## 더 작은 값에서도 S 이상이 되는지 알아봐야 함 s += 1 else: ## (s, e) = (n-1, n) 이 될 때 까지 if e != N: e += 1 else: s +=..

알고리즘

<백준> 2470번 파이썬 알고리즘 [투포인터]

import sys n = int(input()) arr = list(map(int, input().split(' '))) start = 0 end = n - 1 arr.sort() res = sys.maxsize final = [] while start < end: total = arr[start] + arr[end] if abs(total) < res: res = abs(total) final = [arr[start], arr[end]] if total < 0: start += 1 else: end -= 1 print(final[0], final[1])

알고리즘

<백준> 3273번 자바 알고리즘 [투 포인터]

import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = in.nextInt(); } int x = in.nextInt(); int a1 = 0; int a2 = arr.length - 1; int cnt = 0; Arrays.sort(arr); while(a1 < a2){ int sum = arr[a1] + arr[a2]; if(sum == x){ cnt += 1; a1 += 1; a2 -= 1; }els..

알고리즘

<백준> 2805번 자바 알고리즘 [이분 탐색]

import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int[] arr = new int[n]; int max = 0; // arr 셋팅 for(int i = 0; i max){ max = arr[i]; } } int min = 0; while(min mid)..

알고리즘

<백준> 1920번 자바 알고리즘 [이분 탐색]

import java.util.*; public class Main { static int N, M; static int[] arr; public static void main(String[] args) { Scanner in = new Scanner(System.in); N = in.nextInt(); arr = new int[N]; // A 초기화 for(int i=0; i < N; i++){ arr[i] = in.nextInt(); } Arrays.sort(arr); // 정렬 하기 M = in.nextInt(); for(int j=0; j < M; j++){ int low = 0; int high = N - 1; int m = in.nextInt(); System.out.println(binary..

changha.
'알고리즘' 카테고리의 글 목록 (4 Page)