본문 바로가기

알고리즘 & 자료구조569

백준 1308 package Cho; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Calendar; import java.util.StringTokenizer; public class Main1308 { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int startYear = I.. 2021. 11. 15.
백준 1296 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main1296 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String teamName = br.readLine(); int N = Integer.parseInt(br.readLine()); int[] rank = new int[N]; String[] str = new Stri.. 2021. 11. 14.
백준 1268 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main1268 { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int N = Integer.parseInt(br.readLine()); //학생 수 int[][] leader = new int[N][5]; for(int i=0; i 2021. 11. 13.
재귀 함수 (3) 미로찾기 현재 위치에서 출구까지 가는 경로가 있으려면 현재 위치가 출구이거나 혹은 이웃한 셀들 중 하나에서 현재 위치를 지나지 않고 출구까지 가는 경로가 있거나 boolean findPath(x,y) if(x,y) is the exit return true; else for each neighbouring cell (x', y') of (x,y) do if(x',y') is on the pathway if(findPath(x',y') return true; return false 미로찾기는 답이 Yes or No인 문제이다. boolean findPath(x,y) if(x,y) is either on the wall or a visited cell return false; else if (x,y) as .. 2021. 11. 11.
재귀함수 (2) 순환 알고리즘 설계 적어도 하나의 basecase, 즉 순환되지 않고 종료되는 case가 있어야 한다. 모든 case는 결국 base case로 수렴해야 한다. 암시적(implicit) 매개변수를 명시적(explicit) 매개변수로 바꾸어라. 순차탐색 int search(int[] data, int n, int target){ for(int i=0; i end) return -1; else if (target == data[begin]) return begin; else return search(data, begin+1, end, target); } 이 함수의 미션은 data[begin]에서 data[end] 사이에서 target을 검색한다. 즉, 검색 구간의 시작점을 명시적(explicit)으로 지정한.. 2021. 11. 10.
재귀함수 (1) 자기 자신을 호출하는 함수 void func(...){ ... func(...); ... } recursion이 항상 무한루프에 빠지는 것은 아니고 종료 조건을 알맞게 설정해주면 원하는 만큼 함수 호출이 가능하다. 적어도 하나의 recursion에 빠지지 않는 경우가 존재해야 한다. → 종료 조건 recursion을 반복하다면 결국 종료조건으로 수렴해야 한다. 1~N까지의 합 public static int func(int n){ if(n == 0) return 0; else return n + func(n-1); } 팩토리얼 0! = 1 n! = n x (n-1) ! (n>0) public static int factorial(int n){ if(n == 0) return 1; else n * fact.. 2021. 11. 9.