본문 바로가기

프로그래밍

(56)
너비 우선 탐색(BFS: Breadth First Search) 너비 우선 탐색(BFS)란? 시작 정점으로부터 가까운 정점을 먼저 방문하고 멀리 떨어져 있는 정점을 나중에 방문하는 순회 방법 큐를 이용해서 지금 위치에서 갈 수 있는 곳을 모두 큐에 넣는 방식 큐에 넣을 때 방문했다고 체크 모든 가중치가 1일때 최단 거리를 구하는 알고리즘 너비 우선 탐색 알고리즘 breadth_first_search(v) v를 방문되었따고 표시; 큐 Q에 정점 v를 삽입; while (not is_empty(Q)) do Q에서 정점 w를 삭제; for all u ∈ (w에 인접한 정점) do if (u가 아직 방문되지 않았으면) then u를 큐에 삽입; u를 방문되었다고 표시;
에라토스테네스의 체 에라토스테네스의 체에라토스테네스의 체는 합성수를 지우는 방식으로 소수를 찾는 방법이다.소수(Prime Number)소수는 자신보다 작은 두 개의 자연수를 곱하여 만들 수 없는 1보다 큰 자연수이다. 약수로 1과 자기 자신만을 가지는 정수라고 할 수 있다.합성수1과 자기 자신 이외의 약수를 가진 정수로 두개 이상의 소수의 곱이다.(비소수)동작 과정1은 제거지워지지 않은 수 중 제일 작은 2를 소수로 채택하고 2의 배수를 모두 지운다.지워지지 않은 수 중 제일 작은 3을 소수로 채택하고 나머지 3의 배수를 모두 지운다.지워지지 않은 수 중 제일 작은 수 중 제일 작은 5를 소수로 채택하고 나머지 5의 배수를 모두 지운다.이러한 과정을 반복한다.코드(Python)n = int(input()) a = [Fals..
조합과 순열 1. 조합집합에서 일부 원소를 취해 부분집합을 만드는 방법서로 다른 n개의 원소에서 중복을 허용하지 않고 k개를 뽑는 경우의 수순서가 없음- 조합의 수(Python)1234def combination(n, r): if n == r or r == 0: return 1 else: return combination(n-1, r-1) + combination(n-1, r)combination(5, 3)Colored by Color Scriptercs- 조합 출력(Python)123456789101112def combination(comArr, n, r, index, target, arr): if r==0:print(comArr) elif target==n: return else: comArr[index] = a..
(파이썬) 백준 알고리즘 10422번 괄호 문제풀이 (Python) 123456789101112131415161718192021def binomial(n, k): res = 1 for i in range(k): res = res*(n-i) res = res//(i+1) return res def catalan(n): c = binomial(2*n, n) return c//(n+1) T = int(input()) for i in range(T): n = int(input()) if n % 2 != 0: print(0) continue print(catalan(n//2)%1000000007)cs키워드 (Keyword)키워드 카탈란수 참조https://ko.wikipedia.org/wiki/%EC%B9%B4%ED%83%88%EB%9E%91_%EC%88..
(자바) 백준 알고리즘 10250번 ACM 호텔 문제풀이 (Java) 12345678910111213141516171819202122import java.util.Scanner; public class AcmHotel { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for(int i = 0; i
(자바) 백준 알고리즘 2292번 벌집 문제풀이 (Java)12345678910111213141516171819202122public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int target = sc.nextInt(); int cnt = 1; int range = 1; int tmp = 1; while(true) { if(range >= target) { break; } tmp = 6*(cnt++); range += tmp; } System.out.println(cnt); }}Colored by Color Scriptercs1칸 - 12칸 - 2~73칸 - 8..
(파이썬) 백준 알고리즘 5622번 다이얼 문제풀이 (Python) 12345678dial = ['ABC', 'DEF', 'GHI', 'JKL', 'MNO', 'PQRS', 'TUV', 'WXYZ']a = input()ret = 0for j in range(len(a)): for i in dial: if a[j] in i: ret += dial.index(i)+3print(ret)Colored by Color Scriptercs문제 출처https://www.acmicpc.net/problem/5622
(파이썬) 백준 알고리즘 2908번 상수 문제풀이 (Python) 123456A, B = input().split()A = A[::-1]B = B[::-1]ret = max(A, B) print(ret)cs알고리즘 분류문자열 문제 출처https://www.acmicpc.net/problem/2908