├── 1065.java ├── 10816.java ├── 10818.java ├── 10819.java ├── 10971.java ├── 10989.java ├── 11047.java ├── 11399.java ├── 11404.java ├── 1157.java ├── 11650.java ├── 11651.java ├── 1316.java ├── 1330.java ├── 13305.java ├── 13549.py ├── 13913.py ├── 1463.java ├── 1541.java ├── 15552.java ├── 15552_2.java ├── 16235.py ├── 16953.java ├── 1697.java ├── 1697.py ├── 18108.java ├── 18312.java ├── 18870.java ├── 1920.java ├── 1946.java ├── 2178.java ├── 2438.java ├── 2439.java ├── 2525.java ├── 2529.java ├── 2562.java ├── 2562_2.java ├── 2577.java ├── 2579.java ├── 2588.java ├── 2606.java ├── 2665.py ├── 2675.java ├── 2739.java ├── 2750.java ├── 2751.java ├── 2805.java ├── 2884.java ├── 2961.java ├── 2961.py ├── 3003.java ├── 4344.java ├── 6987.py ├── 9184.java ├── Main.java └── README.md /1065.java: -------------------------------------------------------------------------------- 1 | /* 2 | [문제 설명] 3 | X의 각 자릿수가 등차수열을 이룬다면, "한수" 4 | 1부터 N까지의 모든 "한수"를 출력하는 문제 5 | 6 | [예시] 7 | 101 -> 한수 X 8 | 98 -> 한수 O 9 | 7 -> 한수 O 10 | 123 -> 한수 O 11 | 124 -> 한수 X 12 | 13 | [풀이 방법] 14 | * x가 100 미만이라면, 전부 한수 15 | * x가 100 이상 1000 미만이라면, 등차수열인지 계산해봐야 앎 16 | * x가 1000이라면, 한수 아님 17 | */ 18 | 19 | import java.util.*; 20 | import java.io.*; 21 | 22 | public class Main { 23 | public static boolean oneNumber(int x) { 24 | if (x < 100) return true; // 100 미만이면 한수 O 25 | else if (x >= 100 && x < 1000) { // 세자릿수 26 | int x_1 = x % 10; // 일의 자리 27 | int x_2 = (x / 10) % 10; // 십의 자리 28 | int x_3 = (x / 100); // 백의 자리 29 | // 등차수열이라면 한수 O, 아니라면 한수 X 30 | if (x_1 - x_2 == x_2 - x_3) return true; 31 | else return false; 32 | } 33 | else return false; // 1000인 경우 한수 X 34 | } 35 | 36 | public static void main(String[] args) throws IOException { 37 | FastReader fr = new FastReader(); 38 | int n = fr.nextInt(); 39 | int count = 0; 40 | // 1부터 N까지의 수 중에서 한수인 것의 개수를 출력 41 | for (int i = 1; i <= n; i++) { 42 | if (oneNumber(i)) count++; 43 | } 44 | System.out.println(count); 45 | } 46 | public static class FastReader { 47 | BufferedReader br; 48 | StringTokenizer st; 49 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 50 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 51 | String next() { 52 | while (st == null || !st.hasMoreElements()) { 53 | try { st = new StringTokenizer(br.readLine()); } 54 | catch (IOException e) { e.printStackTrace(); } 55 | } 56 | return st.nextToken(); 57 | } 58 | int nextInt() { return Integer.parseInt(next()); } 59 | long nextLong() { return Long.parseLong(next()); } 60 | double nextDouble() { return Double.parseDouble(next()); } 61 | String nextLine() { 62 | String str = ""; 63 | try { str = br.readLine(); } 64 | catch (IOException e) { e.printStackTrace(); } 65 | return str; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /10816.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int lowerBound(int[] arr, int target, int start, int end) { 7 | while (start < end) { 8 | int mid = (start + end) / 2; 9 | if (arr[mid] >= target) end = mid; 10 | else start = mid + 1; 11 | } 12 | return end; 13 | } 14 | 15 | public static int upperBound(int[] arr, int target, int start, int end) { 16 | while (start < end) { 17 | int mid = (start + end) / 2; 18 | if (arr[mid] > target) end = mid; 19 | else start = mid + 1; 20 | } 21 | return end; 22 | } 23 | 24 | // 값이 [left_value, right_value]인 데이터의 개수를 반환하는 함수 25 | public static int countByRange(int[] arr, int leftValue, int rightValue) { 26 | // 유의: lowerBound와 upperBound는 end 변수의 값을 배열의 길이로 설정 27 | int rightIndex = upperBound(arr, rightValue, 0, arr.length); 28 | int leftIndex = lowerBound(arr, leftValue, 0, arr.length); 29 | return rightIndex - leftIndex; 30 | } 31 | 32 | public static void main(String[] args) throws IOException { 33 | FastReader fr = new FastReader(); 34 | 35 | // n개의 데이터가 들어갈 배열 36 | int n = fr.nextInt(); 37 | int[] arr = new int[n]; 38 | 39 | // n개의 데이터 입력받기 40 | for (int i = 0; i < n; i++) { 41 | arr[i] = fr.nextInt(); 42 | } 43 | Arrays.sort(arr); // 오름차순 정렬 44 | 45 | StringWriter stringWriter = new StringWriter(); 46 | BufferedWriter buffWriter = new BufferedWriter(stringWriter); 47 | 48 | // m개의 쿼리 처리 49 | int m = fr.nextInt(); 50 | for (int i = 0; i < m; i++) { 51 | int x = fr.nextInt(); 52 | int result = countByRange(arr, x, x); 53 | buffWriter.write(result + " "); 54 | } 55 | 56 | // Flush the buffer writer 57 | buffWriter.flush(); 58 | System.out.println(stringWriter.getBuffer()); 59 | } 60 | 61 | public static class FastReader { 62 | BufferedReader br; 63 | StringTokenizer st; 64 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 65 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 66 | String next() { 67 | while (st == null || !st.hasMoreElements()) { 68 | try { st = new StringTokenizer(br.readLine()); } 69 | catch (IOException e) { e.printStackTrace(); } 70 | } 71 | return st.nextToken(); 72 | } 73 | int nextInt() { return Integer.parseInt(next()); } 74 | long nextLong() { return Long.parseLong(next()); } 75 | double nextDouble() { return Double.parseDouble(next()); } 76 | String nextLine() { 77 | String str = ""; 78 | try { str = br.readLine(); } 79 | catch (IOException e) { e.printStackTrace(); } 80 | return str; 81 | } 82 | } 83 | } 84 | 85 | /* 86 | 87 | N = 10 (원소의 수) 88 | 각 원소: 6 3 2 10 10 10 -10 -10 7 3 89 | M = 8 (쿼리의 수) 90 | 찾을 원소: 10 9 -5 2 3 4 5 -10 91 | 92 | O(MlogN): 각 쿼리에 대하여 log 시간 복잡도로 탐색을 수행 93 | 94 | import java.io.*; 95 | import java.util.*; 96 | 97 | public class Main { 98 | 99 | public static int lowerBound(int[] arr, int target, int start, int end) { 100 | while (start < end) { 101 | int mid = (start + end) / 2; 102 | if (arr[mid] >= target) end = mid; 103 | else start = mid + 1; 104 | } 105 | return end; 106 | } 107 | 108 | public static int upperBound(int[] arr, int target, int start, int end) { 109 | while (start < end) { 110 | int mid = (start + end) / 2; 111 | if (arr[mid] > target) end = mid; 112 | else start = mid + 1; 113 | } 114 | return end; 115 | } 116 | 117 | // 값이 [left_value, right_value]인 데이터의 개수를 반환하는 함수 118 | public static int countByRange(int[] arr, int leftValue, int rightValue) { 119 | // 유의: lowerBound와 upperBound는 end 변수의 값을 배열의 길이로 설정 120 | int rightIndex = upperBound(arr, rightValue, 0, arr.length); 121 | int leftIndex = lowerBound(arr, leftValue, 0, arr.length); 122 | return rightIndex - leftIndex; 123 | } 124 | 125 | public static void main(String[] args) throws IOException { 126 | Scanner scanner = new Scanner(System.in); 127 | 128 | // 전체 원소 입력받기 129 | int n = scanner.nextInt(); 130 | int[] arr = new int[n]; 131 | for(int i = 0; i < n; i++) { 132 | arr[i] = scanner.nextInt(); 133 | } 134 | // 이진 탐색을 위한 오름차순 정렬 135 | Arrays.sort(arr); 136 | 137 | StringWriter stringWriter = new StringWriter(); 138 | BufferedWriter buffWriter = new BufferedWriter(stringWriter); 139 | 140 | // 전체 쿼리 입력받기 141 | int m = scanner.nextInt(); 142 | String answer = ""; 143 | for(int i = 0; i < m; i++) { 144 | int query = scanner.nextInt(); 145 | int count = countByRange(arr, query, query); 146 | buffWriter.write(count + " "); 147 | } 148 | buffWriter.flush(); 149 | System.out.println(stringWriter.getBuffer()); 150 | } 151 | } 152 | 153 | */ 154 | -------------------------------------------------------------------------------- /10818.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); 7 | int n = fr.nextInt(); 8 | ArrayList arrayList = new ArrayList<>(); 9 | for (int i = 0; i < n; i++) { 10 | int x = fr.nextInt(); 11 | arrayList.add(x); 12 | } 13 | int minValue = Collections.min(arrayList); 14 | int maxValue = Collections.max(arrayList); 15 | System.out.println(minValue + " " + maxValue); 16 | } 17 | public static class FastReader { 18 | BufferedReader br; 19 | StringTokenizer st; 20 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 21 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 22 | String next() { 23 | while (st == null || !st.hasMoreElements()) { 24 | try { st = new StringTokenizer(br.readLine()); } 25 | catch (IOException e) { e.printStackTrace(); } 26 | } 27 | return st.nextToken(); 28 | } 29 | int nextInt() { return Integer.parseInt(next()); } 30 | long nextLong() { return Long.parseLong(next()); } 31 | double nextDouble() { return Double.parseDouble(next()); } 32 | String nextLine() { 33 | String str = ""; 34 | try { str = br.readLine(); } 35 | catch (IOException e) { e.printStackTrace(); } 36 | return str; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /10819.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static boolean[] visited = new boolean[10]; 7 | public static Stack result = new Stack(); 8 | public static ArrayList arr = new ArrayList(); 9 | public static int n; 10 | public static int maxValue = (int) -1e9; 11 | 12 | public static void dfs(int depth) { 13 | // 각 순열에 대한 처리 14 | if (depth == n) { 15 | // 공식 계산 16 | int current = 0; 17 | for (int i = 0; i < n - 1; i++) { 18 | current += Math.abs(result.get(i) - result.get(i + 1)); 19 | } 20 | if (maxValue < current) { 21 | } 22 | maxValue = Math.max(maxValue, current); 23 | } 24 | // 각 수를 N개 선택하는 모든 순열 계산 25 | for (int i = 0; i < n; i++) { 26 | // 미리 고른 수라면 무시하도록 27 | if (visited[i]) 28 | continue; 29 | // 현재 선택한 수 방문 처리 30 | visited[i] = true; 31 | result.push(arr.get(i)); 32 | dfs(depth + 1); // 재귀 함수 호출 33 | // 현재 선택한 수 방문 처리 취소 34 | visited[i] = false; 35 | result.pop(); 36 | } 37 | } 38 | 39 | public static void main(String[] args) throws IOException { 40 | Scanner scanner = new Scanner(System.in); 41 | n = scanner.nextInt(); 42 | for (int i = 0; i < n; i++) { 43 | arr.add(scanner.nextInt()); 44 | } 45 | dfs(0); 46 | System.out.println(maxValue); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /10971.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static boolean[] visited = new boolean[11]; 7 | public static Stack result = new Stack(); 8 | public static ArrayList> graph = new ArrayList>(); 9 | public static int n; 10 | public static int minValue = (int) 1e9; 11 | 12 | // 2부터 N까지의 수를 하나씩 골라 나열하는 모든 순열을 계산 13 | public static void dfs(int depth) { 14 | if (depth == n - 1) { 15 | // 현재 순열에 대한 처리 16 | int totalCost = 0; 17 | // 1번 노드에서 출발 18 | int cur = 1; 19 | // 현재 순열에 따라서 노드 이동 20 | for (int i = 0; i < n - 1; i++) { 21 | // 다음 이동 노드까지의 비용 계산 22 | int nextNode = result.get(i); 23 | int cost = graph.get(cur).get(nextNode); 24 | if (cost == 0) return; // 이동 불가능하면 무시 25 | // 이동 가능하면, 비용을 더하고 노드 이동 26 | totalCost += cost; 27 | cur = nextNode; 28 | } 29 | // 마지막 노드에서 1로 돌아오는 것이 필수 30 | int cost = graph.get(cur).get(1); 31 | if (cost == 0) return; // 이동 불가능하면 무시 32 | // 이동 가능하면, 비용을 더하고 노드 이동 33 | totalCost += cost; 34 | // 경로의 최소 비용 저장 35 | minValue = Math.min(minValue, totalCost); 36 | return; 37 | } 38 | for (int i = 2; i <= n; i++) { 39 | if (visited[i]) continue; 40 | // 방문 처리 41 | result.push(i); 42 | visited[i] = true; 43 | dfs(depth + 1); // 재귀 함수 호출 44 | // 방문 처리 해제 45 | result.pop(); 46 | visited[i] = false; 47 | } 48 | } 49 | 50 | public static void main(String[] args) throws IOException { 51 | Scanner scanner = new Scanner(System.in); 52 | n = scanner.nextInt(); 53 | for (int i = 0; i <= n; i++) { 54 | graph.add(new ArrayList()); 55 | } 56 | for (int i = 1; i <= n; i++) { 57 | graph.get(i).add(0); 58 | for (int j = 1; j <= n; j++) { 59 | int cost = scanner.nextInt(); 60 | graph.get(i).add(cost); 61 | } 62 | } 63 | dfs(0); 64 | System.out.println(minValue); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /10989.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | 8 | int[] cnt = new int[10001]; 9 | 10 | int n = fr.nextInt(); 11 | for (int i = 0; i < n; i++) { 12 | int x = fr.nextInt(); 13 | cnt[x]++; 14 | } 15 | 16 | StringWriter stringWriter = new StringWriter(); 17 | BufferedWriter buffWriter = new BufferedWriter(stringWriter); 18 | 19 | for (int i = 0; i <= 10000; i++) { 20 | for (int j = 0; j < cnt[i]; j++) { 21 | buffWriter.write(i + "\n"); 22 | } 23 | } 24 | // Flush the buffer writer 25 | buffWriter.flush(); 26 | System.out.println(stringWriter.getBuffer()); 27 | } 28 | 29 | public static class FastReader { 30 | BufferedReader br; 31 | StringTokenizer st; 32 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 33 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 34 | String next() { 35 | while (st == null || !st.hasMoreElements()) { 36 | try { st = new StringTokenizer(br.readLine()); } 37 | catch (IOException e) { e.printStackTrace(); } 38 | } 39 | return st.nextToken(); 40 | } 41 | int nextInt() { return Integer.parseInt(next()); } 42 | long nextLong() { return Long.parseLong(next()); } 43 | double nextDouble() { return Double.parseDouble(next()); } 44 | String nextLine() { 45 | String str = ""; 46 | try { str = br.readLine(); } 47 | catch (IOException e) { e.printStackTrace(); } 48 | return str; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /11047.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | 5 | public class Main { 6 | public static void main(String[] args) throws IOException { 7 | FastReader fr = new FastReader(); 8 | 9 | int n = fr.nextInt(); // 동전(화폐) 종류 10 | int k = fr.nextInt(); // 거슬러 줄 돈 11 | 12 | ArrayList coins = new ArrayList<>(); 13 | for (int i = 0; i < n; i++) { // N개 만큼의 화폐 입력 14 | int x = fr.nextInt(); 15 | coins.add(x); 16 | } 17 | int result = 0; 18 | for (int i = n - 1; i >= 0; i--) { // 큰 화폐부터 보기 19 | result += k / coins.get(i); // 해당 화폐로 몇 개 가능? 20 | k %= coins.get(i); 21 | } 22 | System.out.println(result); 23 | } 24 | 25 | public static class FastReader { 26 | BufferedReader br; 27 | StringTokenizer st; 28 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 29 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 30 | String next() { 31 | while (st == null || !st.hasMoreElements()) { 32 | try { st = new StringTokenizer(br.readLine()); } 33 | catch (IOException e) { e.printStackTrace(); } 34 | } 35 | return st.nextToken(); 36 | } 37 | int nextInt() { return Integer.parseInt(next()); } 38 | long nextLong() { return Long.parseLong(next()); } 39 | double nextDouble() { return Double.parseDouble(next()); } 40 | String nextLine() { 41 | String str = ""; 42 | try { str = br.readLine(); } 43 | catch (IOException e) { e.printStackTrace(); } 44 | return str; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /11399.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class Point implements Comparable { 5 | public int x; 6 | public int y; 7 | public Point (int x, int y) { 8 | this.x = x; 9 | this.y = y; 10 | } 11 | @Override 12 | public int compareTo(Point other) { 13 | if (this.y != other.y) { 14 | return this.y - other.y; 15 | } 16 | else { 17 | return this.x - other.x; 18 | } 19 | } 20 | @Override 21 | public String toString() { 22 | return x + " " + y; 23 | } 24 | } 25 | 26 | 27 | public class Main { 28 | public static void main(String[] args) throws IOException { 29 | FastReader fr = new FastReader(); 30 | 31 | int n = fr.nextInt(); // 총 사람의 수 32 | ArrayList times = new ArrayList<>(); 33 | 34 | // 각 사람마다 처리 시간 입력받기 35 | for (int i = 0; i < n; i++) { 36 | int time = fr.nextInt(); 37 | times.add(time); 38 | } 39 | 40 | Collections.sort(times); // 오름차순 정렬 41 | int result = 0; 42 | for (int i = 0; i < n; i++) { // 처리 시간을 하나씩 보면서 43 | // i번째 사람을 처리하면, 뒤쪽에는 n - i명이 기다리므로 44 | // 거기에 자기 자신을 처리하는 시간 더하기 45 | result += times.get(i) * (n - i - 1) + times.get(i); 46 | } 47 | System.out.println(result); 48 | } 49 | 50 | public static class FastReader { 51 | BufferedReader br; 52 | StringTokenizer st; 53 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 54 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 55 | String next() { 56 | while (st == null || !st.hasMoreElements()) { 57 | try { st = new StringTokenizer(br.readLine()); } 58 | catch (IOException e) { e.printStackTrace(); } 59 | } 60 | return st.nextToken(); 61 | } 62 | int nextInt() { return Integer.parseInt(next()); } 63 | long nextLong() { return Long.parseLong(next()); } 64 | double nextDouble() { return Double.parseDouble(next()); } 65 | String nextLine() { 66 | String str = ""; 67 | try { str = br.readLine(); } 68 | catch (IOException e) { e.printStackTrace(); } 69 | return str; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /11404.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static final int INF = (int) 1e9; 7 | 8 | public static void main(String[] args) { 9 | FastReader fr = new FastReader(); 10 | int n = fr.nextInt(); // 노드 개수 11 | int m = fr.nextInt(); // 간선 개수 12 | int[][] dist = new int[n + 1][n + 1]; 13 | for (int i = 1; i <= n; i++) { 14 | for (int j = 1; j <= n; j++) { 15 | dist[i][j] = INF; 16 | } 17 | } 18 | for (int i = 1; i <= n; i++) { 19 | dist[i][i] = 0; 20 | } 21 | for (int i = 0; i < m; i++) { 22 | int a = fr.nextInt(); 23 | int b = fr.nextInt(); 24 | int cost = fr.nextInt(); 25 | dist[a][b] = Math.min(dist[a][b], cost); 26 | } 27 | 28 | for (int k = 1; k <= n; k++) { 29 | for (int i = 1; i <= n; i++) { 30 | for (int j = 1; j <= n; j++) { 31 | dist[i][j] = Math.min(dist[i][j], 32 | dist[i][k] + dist[k][j]); 33 | } 34 | } 35 | } 36 | 37 | for (int i = 1; i <= n; i++) { 38 | for (int j = 1; j <= n; j++) { 39 | if (dist[i][j] == INF) { 40 | System.out.print("0 "); 41 | } 42 | else { 43 | System.out.print(dist[i][j] + " "); 44 | } 45 | } 46 | System.out.println(); 47 | } 48 | 49 | } 50 | public static class FastReader { 51 | BufferedReader br; 52 | StringTokenizer st; 53 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 54 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 55 | String next() { 56 | while (st == null || !st.hasMoreElements()) { 57 | try { st = new StringTokenizer(br.readLine()); } 58 | catch (IOException e) { e.printStackTrace(); } 59 | } 60 | return st.nextToken(); 61 | } 62 | int nextInt() { return Integer.parseInt(next()); } 63 | long nextLong() { return Long.parseLong(next()); } 64 | double nextDouble() { return Double.parseDouble(next()); } 65 | String nextLine() { 66 | String str = ""; 67 | try { str = br.readLine(); } 68 | catch (IOException e) { e.printStackTrace(); } 69 | return str; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /1157.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | String s = fr.next(); 8 | int[] counter = new int[26]; 9 | for (int i = 0; i < s.length(); i++) { 10 | char x = Character.toLowerCase(s.charAt(i)); 11 | int index = x - 'a'; // 인덱스 계산 12 | counter[index]++; // 해당 알파벳의 개수 카운트 13 | } 14 | // 가장 개수(count)가 많은 알파벳 찾기 15 | int maxCount = 0; 16 | int maxIndex = -1; 17 | for (int i = 0; i < 26; i++) { 18 | if (counter[i] > maxCount) { 19 | maxCount = counter[i]; 20 | maxIndex = i; 21 | } 22 | } 23 | // 가장 개수(count)가 많은 찾기 2개 이상인지 확인 24 | boolean duplicated = false; 25 | for (int i = 0; i < 26; i++) { 26 | if (counter[i] == maxCount && i != maxIndex) { 27 | duplicated = true; 28 | } 29 | } 30 | if (duplicated) System.out.println("?"); 31 | else System.out.println(Character.toUpperCase((char) (maxIndex + 'a'))); 32 | } 33 | public static class FastReader { 34 | BufferedReader br; 35 | StringTokenizer st; 36 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 37 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 38 | String next() { 39 | while (st == null || !st.hasMoreElements()) { 40 | try { st = new StringTokenizer(br.readLine()); } 41 | catch (IOException e) { e.printStackTrace(); } 42 | } 43 | return st.nextToken(); 44 | } 45 | int nextInt() { return Integer.parseInt(next()); } 46 | long nextLong() { return Long.parseLong(next()); } 47 | double nextDouble() { return Double.parseDouble(next()); } 48 | String nextLine() { 49 | String str = ""; 50 | try { str = br.readLine(); } 51 | catch (IOException e) { e.printStackTrace(); } 52 | return str; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /11650.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class Point implements Comparable { 5 | public int x; 6 | public int y; 7 | public Point (int x, int y) { 8 | this.x = x; 9 | this.y = y; 10 | } 11 | @Override 12 | public int compareTo(Point other) { 13 | if (this.x != other.x) { 14 | return this.x - other.x; 15 | } 16 | else { 17 | return this.y - other.y; 18 | } 19 | } 20 | @Override 21 | public String toString() { 22 | return x + " " + y; 23 | } 24 | } 25 | 26 | 27 | public class Main { 28 | public static void main(String[] args) throws IOException { 29 | FastReader fr = new FastReader(); 30 | 31 | int n = fr.nextInt(); 32 | 33 | ArrayList arrayList = new ArrayList<>(); 34 | for (int i = 0; i < n; i++) { 35 | int x = fr.nextInt(); 36 | int y = fr.nextInt(); 37 | arrayList.add(new Point(x, y)); 38 | } 39 | 40 | Collections.sort(arrayList); 41 | for (int i = 0; i < n; i++) { 42 | System.out.println(arrayList.get(i)); 43 | } 44 | } 45 | 46 | public static class FastReader { 47 | BufferedReader br; 48 | StringTokenizer st; 49 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 50 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 51 | String next() { 52 | while (st == null || !st.hasMoreElements()) { 53 | try { st = new StringTokenizer(br.readLine()); } 54 | catch (IOException e) { e.printStackTrace(); } 55 | } 56 | return st.nextToken(); 57 | } 58 | int nextInt() { return Integer.parseInt(next()); } 59 | long nextLong() { return Long.parseLong(next()); } 60 | double nextDouble() { return Double.parseDouble(next()); } 61 | String nextLine() { 62 | String str = ""; 63 | try { str = br.readLine(); } 64 | catch (IOException e) { e.printStackTrace(); } 65 | return str; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /11651.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class Point implements Comparable { 5 | public int x; 6 | public int y; 7 | public Point (int x, int y) { 8 | this.x = x; 9 | this.y = y; 10 | } 11 | @Override 12 | public int compareTo(Point other) { 13 | if (this.y != other.y) { 14 | return this.y - other.y; 15 | } 16 | else { 17 | return this.x - other.x; 18 | } 19 | } 20 | @Override 21 | public String toString() { 22 | return x + " " + y; 23 | } 24 | } 25 | 26 | 27 | public class Main { 28 | public static void main(String[] args) throws IOException { 29 | FastReader fr = new FastReader(); 30 | 31 | int n = fr.nextInt(); 32 | 33 | ArrayList arrayList = new ArrayList<>(); 34 | for (int i = 0; i < n; i++) { 35 | int x = fr.nextInt(); 36 | int y = fr.nextInt(); 37 | arrayList.add(new Point(x, y)); 38 | } 39 | 40 | Collections.sort(arrayList); 41 | for (int i = 0; i < n; i++) { 42 | System.out.println(arrayList.get(i)); 43 | } 44 | } 45 | 46 | public static class FastReader { 47 | BufferedReader br; 48 | StringTokenizer st; 49 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 50 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 51 | String next() { 52 | while (st == null || !st.hasMoreElements()) { 53 | try { st = new StringTokenizer(br.readLine()); } 54 | catch (IOException e) { e.printStackTrace(); } 55 | } 56 | return st.nextToken(); 57 | } 58 | int nextInt() { return Integer.parseInt(next()); } 59 | long nextLong() { return Long.parseLong(next()); } 60 | double nextDouble() { return Double.parseDouble(next()); } 61 | String nextLine() { 62 | String str = ""; 63 | try { str = br.readLine(); } 64 | catch (IOException e) { e.printStackTrace(); } 65 | return str; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /1316.java: -------------------------------------------------------------------------------- 1 | /* 2 | "그룹 단어"란, 단어에 존재하는 모든 문자에 대해서, 3 | 각 문자가 연속해서 나타나는 경우만을 말한다. 4 | 5 | aaaaabbbbbccccc: 그룹 단어 O 6 | aaaaabbbbbccccca: 그룹 단어 X 7 | */ 8 | 9 | import java.util.*; 10 | import java.io.*; 11 | 12 | public class Main { 13 | public static void main(String[] args) throws IOException { 14 | FastReader fr = new FastReader(); 15 | int n = fr.nextInt(); 16 | int result = 0; 17 | for (int testCase = 0; testCase < n; testCase++) { 18 | String s = fr.next(); 19 | boolean groupWord = true; 20 | // 이미 처리된 문자를 담기 위한 집합(Set) 자료형 21 | HashSet hashSet = new HashSet(); 22 | hashSet.add(s.charAt(0)); 23 | // 하나씩 문자를 확인하며 24 | for (int i = 1; i < s.length(); i++) { 25 | // 이미 처리된 문자라면 26 | if (hashSet.contains(s.charAt(i))) { 27 | // 바로 앞에 있는 문자랑 같지 않다면(그룹이 아니라면) 28 | if (s.charAt(i) != s.charAt(i - 1)) { 29 | groupWord = false; 30 | } 31 | } 32 | else hashSet.add(s.charAt(i)); 33 | } 34 | if (groupWord) result++; 35 | } 36 | System.out.println(result); 37 | } 38 | public static class FastReader { 39 | BufferedReader br; 40 | StringTokenizer st; 41 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 42 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 43 | String next() { 44 | while (st == null || !st.hasMoreElements()) { 45 | try { st = new StringTokenizer(br.readLine()); } 46 | catch (IOException e) { e.printStackTrace(); } 47 | } 48 | return st.nextToken(); 49 | } 50 | int nextInt() { return Integer.parseInt(next()); } 51 | long nextLong() { return Long.parseLong(next()); } 52 | double nextDouble() { return Double.parseDouble(next()); } 53 | String nextLine() { 54 | String str = ""; 55 | try { str = br.readLine(); } 56 | catch (IOException e) { e.printStackTrace(); } 57 | return str; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /1330.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | int a = scanner.nextInt(); 7 | int b = scanner.nextInt(); 8 | if (a > b) System.out.println(">"); 9 | else if (a < b) System.out.println("<"); 10 | else if (a == b) System.out.println("=="); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /13305.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class Point implements Comparable { 5 | public int x; 6 | public int y; 7 | 8 | public Point (int x, int y) { 9 | this.x = x; 10 | this.y = y; 11 | } 12 | 13 | @Override 14 | public int compareTo(Point other) { 15 | if (this.y != other.y) { 16 | return this.y - other.y; 17 | } 18 | else { 19 | return this.x - other.x; 20 | } 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return x + " " + y; 26 | } 27 | } 28 | 29 | public class Main { 30 | public static void main(String[] args) { 31 | FastReader fr = new FastReader(); 32 | int n = fr.nextInt(); 33 | ArrayList dist = new ArrayList<>(); 34 | for (int i = 0; i < n - 1; i++) { 35 | dist.add(fr.nextInt()); 36 | } 37 | ArrayList cost = new ArrayList<>(); 38 | for (int j = 0; j < n; j++) { 39 | cost.add(fr.nextInt()); 40 | } 41 | 42 | // 주유 비용(cost) 배열의 값이 내림차순이 되도록 반환 43 | // [5, 2, 4, 1] -> [5, 2, 2, 1] 44 | int minCost = cost.get(0); 45 | for (int i = 0; i < n; i++) { 46 | minCost = Math.min(minCost, cost.get(i)); 47 | cost.set(i, minCost); 48 | } 49 | 50 | // 도로당 이동 비용의 합 계산 51 | long answer = 0; 52 | for (int i = 0; i < n - 1; i++) { 53 | answer += (long) dist.get(i) * cost.get(i); 54 | } 55 | System.out.println(answer); 56 | } 57 | 58 | public static class FastReader { 59 | BufferedReader br; 60 | StringTokenizer st; 61 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 62 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 63 | String next() { 64 | while (st == null || !st.hasMoreElements()) { 65 | try { st = new StringTokenizer(br.readLine()); } 66 | catch (IOException e) { e.printStackTrace(); } 67 | } 68 | return st.nextToken(); 69 | } 70 | int nextInt() { return Integer.parseInt(next()); } 71 | long nextLong() { return Long.parseLong(next()); } 72 | double nextDouble() { return Double.parseDouble(next()); } 73 | String nextLine() { 74 | String str = ""; 75 | try { str = br.readLine(); } 76 | catch (IOException e) { e.printStackTrace(); } 77 | return str; 78 | } 79 | } 80 | } 81 | 82 | /* 83 | 84 | 거리: 2 3 1 2 85 | 주유 비용: 5 2 4 7 8 86 | 87 | 88 | 거리:2 거리:3 거리:1 거리:2 89 | [5원] [2원] [4원] [7원] [8원] 90 | 91 | => "뒤에 있는" 자기보다 비싼 주유소를 미리 "결제"한다. 92 | 93 | [핵심] 비오름차순 처리하기 94 | 95 | 예시 1) 96 | 주유 비용: 5 2 4 7 8 97 | 실제 주유 비용: 5 2 2 2 2 98 | 99 | 예시 2) 100 | 거리: 2 3 1 2 101 | 주유 비용: 7 5 8 4 9 102 | 실제 주유 비용: 7 5 5 4 4 103 | 104 | 답: (7 * 2) + (5 * 3) + (5 * 1) + (4 * 2) 105 | [아이디어 핵심] 비오름차순으로 바꾸어주기 106 | 107 | import java.io.*; 108 | import java.util.*; 109 | 110 | public class Main { 111 | 112 | public static void main(String[] args) { 113 | Scanner scanner = new Scanner(System.in); 114 | 115 | // 전체 주유소의 수 116 | int n = scanner.nextInt(); 117 | 118 | int[] distances = new int[n - 1]; 119 | int[] costs = new int[n]; 120 | 121 | // 전체 거리(distance) 입력받기 122 | for (int i = 0; i < n - 1; i++) { 123 | distances[i] = scanner.nextInt(); 124 | } 125 | // 전체 비용(cost) 입력받기 126 | for (int i = 0; i < n; i++) { 127 | costs[i] = scanner.nextInt(); 128 | } 129 | 130 | // 실제 비용은 비오름차순으로 재정의 131 | int[] realCosts = new int[n]; 132 | int minValue = costs[0]; 133 | for (int i = 0; i < n; i++) { 134 | if (minValue > costs[i]) minValue = costs[i]; 135 | realCosts[i] = minValue; 136 | } 137 | 138 | // 실제 주유 비용 계산 139 | long result = 0; 140 | for (int i = 0; i < n - 1; i++) { 141 | result += (long) distances[i] * realCosts[i]; 142 | } 143 | System.out.println(result); 144 | } 145 | } 146 | 147 | */ 148 | -------------------------------------------------------------------------------- /13549.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, k = map(int, input().split()) 4 | visited = [0] * 100001 5 | 6 | queue = deque() 7 | queue.append(n) 8 | visited[n] = 1 9 | 10 | while queue: 11 | current = queue.popleft() 12 | # 2의 제곱으로 10만 이상이 되기 위해 최대 16~17회이므로 가능한 방법 13 | temp = current * 2 14 | while temp <= 100000: 15 | if visited[temp] == 0: 16 | visited[temp] = visited[current] 17 | queue.append(temp) 18 | if temp == 0: break 19 | temp *= 2 20 | if current - 1 >= 0: 21 | if visited[current - 1] == 0: 22 | visited[current - 1] = visited[current] + 1 23 | queue.append(current - 1) 24 | if current + 1 <= 100000: 25 | if visited[current + 1] == 0: 26 | visited[current + 1] = visited[current] + 1 27 | queue.append(current + 1) 28 | 29 | 30 | print(visited[k] - 1) 31 | 32 | """ 33 | import heapq 34 | 35 | n, k = map(int, input().split()) 36 | distance = [int(1e9)] * 100001 37 | 38 | q = [] 39 | heapq.heappush(q, (0, n)) 40 | distance[n] = 0 41 | 42 | while q: 43 | dist, current = heapq.heappop(q) 44 | if current - 1 >= 0: 45 | if distance[current - 1] > dist + 1: 46 | distance[current - 1] = dist + 1 47 | heapq.heappush(q, (dist + 1, current - 1)) 48 | if current + 1 <= 100000: 49 | if distance[current + 1] > dist + 1: 50 | distance[current + 1] = dist + 1 51 | heapq.heappush(q, (dist + 1, current + 1)) 52 | if current * 2 <= 100000: 53 | if distance[current * 2] > dist: 54 | distance[current * 2] = dist 55 | heapq.heappush(q, (dist, current * 2)) 56 | 57 | print(distance[k]) 58 | """ 59 | -------------------------------------------------------------------------------- /13913.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, k = map(int, input().split()) 4 | visited = [0] * 100001 5 | prev = [-1] * 100001 # 자신의 이전 노드 6 | 7 | queue = deque() 8 | queue.append(n) 9 | visited[n] = 1 10 | 11 | while queue: 12 | current = queue.popleft() 13 | if current - 1 >= 0: 14 | if visited[current - 1] == 0: 15 | visited[current - 1] = visited[current] + 1 16 | prev[current - 1] = current # 경로 추적을 위해 17 | queue.append(current - 1) 18 | if current + 1 <= 100000: 19 | if visited[current + 1] == 0: 20 | visited[current + 1] = visited[current] + 1 21 | prev[current + 1] = current # 경로 추적을 위해 22 | queue.append(current + 1) 23 | if current * 2 <= 100000: 24 | if visited[current * 2] == 0: 25 | visited[current * 2] = visited[current] + 1 26 | prev[current * 2] = current # 경로 추적을 위해 27 | queue.append(current * 2) 28 | 29 | # 경로 추적 결과 출력 30 | result = [k] 31 | cur = k 32 | while prev[cur] != -1: 33 | cur = prev[cur] 34 | result.append(cur) 35 | 36 | print(visited[k] - 1) # 최단 거리 출력 37 | for x in reversed(result): # 경로 출력 38 | print(x, end=" ") 39 | -------------------------------------------------------------------------------- /1463.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static int[] dp = new int[1000001]; 7 | public static int n; 8 | 9 | public static int solve(int x) { 10 | // 종료 조건(초기항) 11 | if (x == 1) return 0; 12 | // 이미 계산한 적 있는 값이라면 13 | if (dp[x] != 0) return dp[x]; 14 | // 그렇지 않다면, 점화식 계산 15 | int result = (int) 1e9; 16 | if (x % 3 == 0) result = Math.min(result, solve(x / 3) + 1); 17 | if (x % 2 == 0) result = Math.min(result, solve(x / 2) + 1); 18 | result = Math.min(result, solve(x - 1) + 1); 19 | dp[x] = result; // 계산한 결과는 저장 20 | return result; 21 | } 22 | 23 | public static void main(String[] args) throws IOException { 24 | Scanner scanner = new Scanner(System.in); 25 | n = scanner.nextInt(); 26 | System.out.println(solve(n)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /1541.java: -------------------------------------------------------------------------------- 1 | /* 2 | 90+30-20+50-30+60-70+30+20 3 | => (90+30)-(20+50)-(30+60)-(70+30+20) 4 | => 첫째 - 앞 부분까지는 다 더해주고, 뒷 부분은 모두 빼준다. 5 | */ 6 | 7 | import java.io.*; 8 | import java.util.*; 9 | 10 | public class Main { 11 | 12 | public static void main(String[] args) { 13 | Scanner scanner = new Scanner(System.in); 14 | String data = scanner.next(); 15 | int result = 0; 16 | 17 | int minusIndex = data.indexOf("-"); 18 | if (minusIndex == -1) { // 마이너스(-)가 없는 경우 19 | String[] arr = data.split("\\+"); 20 | for (String x: arr) { 21 | result += Integer.parseInt(x); 22 | } 23 | } 24 | else { 25 | // 첫째 마이너스(-) 앞 부분 문자열 26 | String add = data.substring(0, minusIndex); 27 | // 첫째 마이너스(-) 뒤 부분 문자열 28 | String minus = data.substring(minusIndex + 1); 29 | // 앞 부분은 모두 더하기 30 | String[] arr = add.split("\\+"); 31 | for (String x: arr) { 32 | result += Integer.parseInt(x); 33 | } 34 | // 뒤 부분은 모두 빼기 35 | arr = minus.split("-"); 36 | for (String x: arr) { 37 | String[] subArr = x.split("\\+"); 38 | for (String y: subArr) { 39 | result -= Integer.parseInt(y); 40 | } 41 | } 42 | } 43 | System.out.println(result); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /15552.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | 8 | StringWriter stringWriter = new StringWriter(); 9 | BufferedWriter buffWriter = new BufferedWriter(stringWriter); 10 | 11 | int testCases = fr.nextInt(); 12 | for (int tc = 0; tc < testCases; tc++) { 13 | int a = fr.nextInt(); 14 | int b = fr.nextInt(); 15 | buffWriter.write((a + b) + "\n"); 16 | } 17 | // Flush the buffer writer 18 | buffWriter.flush(); 19 | System.out.println(stringWriter.getBuffer()); 20 | } 21 | 22 | 23 | public static class FastReader { 24 | BufferedReader br; 25 | StringTokenizer st; 26 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 27 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 28 | String next() { 29 | while (st == null || !st.hasMoreElements()) { 30 | try { st = new StringTokenizer(br.readLine()); } 31 | catch (IOException e) { e.printStackTrace(); } 32 | } 33 | return st.nextToken(); 34 | } 35 | int nextInt() { return Integer.parseInt(next()); } 36 | long nextLong() { return Long.parseLong(next()); } 37 | double nextDouble() { return Double.parseDouble(next()); } 38 | String nextLine() { 39 | String str = ""; 40 | try { str = br.readLine(); } 41 | catch (IOException e) { e.printStackTrace(); } 42 | return str; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /15552_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 8 | int t = fr.nextInt(); 9 | for (int testCase = 0; testCase < t; testCase++) { 10 | int a = fr.nextInt(); 11 | int b = fr.nextInt(); 12 | bw.write(a + b + "\n"); 13 | } 14 | bw.flush(); 15 | } 16 | public static class FastReader { 17 | BufferedReader br; 18 | StringTokenizer st; 19 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 20 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 21 | String next() { 22 | while (st == null || !st.hasMoreElements()) { 23 | try { st = new StringTokenizer(br.readLine()); } 24 | catch (IOException e) { e.printStackTrace(); } 25 | } 26 | return st.nextToken(); 27 | } 28 | int nextInt() { return Integer.parseInt(next()); } 29 | long nextLong() { return Long.parseLong(next()); } 30 | double nextDouble() { return Double.parseDouble(next()); } 31 | String nextLine() { 32 | String str = ""; 33 | try { str = br.readLine(); } 34 | catch (IOException e) { e.printStackTrace(); } 35 | return str; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /16235.py: -------------------------------------------------------------------------------- 1 | """ 2 | * 문제에서 제시된 N은 최대 10이므로, 최대 10 X 10 크기의 맵이 될 것이다. 3 | * 문제에서 요구하는 내용을 그대로 구현해야 하는 [시뮬레이션 유형]의 문제다. 4 | * 인접한 위치는 8개로 간주한다. 5 | 1. (r-1, c-1) 6 | 2. (r-1, c) 7 | 3. (r-1, c+1) 8 | 4. (r, c-1) 9 | 5. (r, c+1) 10 | 6. (r+1, c-1) 11 | 7. (r+1, c) 12 | 8. (r+1, c+1) 13 | """ 14 | 15 | n, m, k = map(int, input().split()) 16 | 17 | # 현재 양분 정보로, 처음에는 5씩 들어있다. 18 | energy = [[5] * n for _ in range(n)] 19 | # 겨울에 각 위치에 추가할 양분 정보 입력 20 | arr = [] 21 | for _ in range(n): 22 | line = list(map(int, input().split())) 23 | arr.append(line) 24 | # N X N 크기의 격자에 각각 나무(tree)를 리스트로 표현 25 | trees = [[[] for j in range(n)] for i in range(n)] 26 | for _ in range(m): 27 | # 각 나무의 (x, y) 좌표와 나이(age) 정보 입력 28 | x, y, age = map(int, input().split()) 29 | # 배열의 인덱스는 0부터 출발하므로 1씩 빼서 넣기 30 | trees[x - 1][y - 1].append(age) 31 | 32 | # 인접한 8가지 방향 33 | dx = [-1, -1, -1, 0, 0, 1, 1, 1] 34 | dy = [-1, 0, 1, -1, 1, -1, 0, 1] 35 | 36 | # N X N 크기의 격자에 각각 죽은 나무(tree)를 리스트로 표현 37 | died = [[[] for j in range(n)] for i in range(n)] 38 | 39 | 40 | def spring(): # 봄일 때 41 | for i in range(n): 42 | for j in range(n): 43 | trees[i][j].sort() # 오름차순 정렬 44 | # 해당 위치의 나무들의 나이를 하나씩 확인 45 | for k in range(len(trees[i][j])): 46 | # 양분이 충분하다면 나이만큼 양분 먹기 47 | if trees[i][j][k] <= energy[i][j]: 48 | energy[i][j] -= trees[i][j][k] 49 | trees[i][j][k] += 1 50 | # 양분이 부족하다면 나이 많은 나무들은 죽고, 반복문 탈출 51 | else: 52 | for age in trees[i][j][k:]: 53 | died[i][j].append(age) 54 | trees[i][j] = trees[i][j][:k] 55 | break 56 | 57 | 58 | def summer(): 59 | for i in range(n): 60 | for j in range(n): 61 | for age in died[i][j]: 62 | energy[i][j] += (age // 2) 63 | died[i][j] = [] 64 | 65 | 66 | def autumn(): 67 | for i in range(n): 68 | for j in range(n): 69 | for age in trees[i][j]: 70 | # 나이가 5의 배수인 경우 71 | if age % 5 == 0: 72 | for k in range(8): 73 | nx = i + dx[k] 74 | ny = j + dy[k] 75 | if nx < 0 or nx >= n or ny < 0 or ny >= n: 76 | continue 77 | trees[nx][ny].append(1) 78 | 79 | 80 | def winter(): 81 | for i in range(n): 82 | for j in range(n): 83 | energy[i][j] += arr[i][j] 84 | 85 | 86 | for _ in range(k): 87 | spring() 88 | summer() 89 | autumn() 90 | winter() 91 | 92 | result = 0 93 | for i in range(n): 94 | for j in range(n): 95 | result += len(trees[i][j]) 96 | print(result) 97 | -------------------------------------------------------------------------------- /16953.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | /* 5 | * B에서 A로 이동한다고 가정한다. 6 | 1) B가 2로 나누어떨어지면, 2로 나누는 것만 가능하다. 7 | 2) B의 일의 자릿수가 1이면, 10으로 나누는 것만 가능하다. 8 | 3) 둘 다 아니라면 이동이 불가능하므로, 종료한다. 9 | */ 10 | 11 | public class Main { 12 | public static void main(String[] args) { 13 | FastReader fr = new FastReader(); 14 | 15 | int a = fr.nextInt(); 16 | int b = fr.nextInt(); 17 | 18 | int result = 1; 19 | boolean flag = false; 20 | while (a <= b) { 21 | if (a == b) { 22 | flag = true; 23 | break; 24 | } 25 | if (b % 2 == 0) b /= 2; 26 | else if (b % 10 == 1) b /= 10; 27 | else break; 28 | result++; 29 | } 30 | if (flag) System.out.println(result); 31 | else System.out.println(-1); 32 | } 33 | 34 | public static class FastReader { 35 | BufferedReader br; 36 | StringTokenizer st; 37 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 38 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 39 | String next() { 40 | while (st == null || !st.hasMoreElements()) { 41 | try { st = new StringTokenizer(br.readLine()); } 42 | catch (IOException e) { e.printStackTrace(); } 43 | } 44 | return st.nextToken(); 45 | } 46 | int nextInt() { return Integer.parseInt(next()); } 47 | long nextLong() { return Long.parseLong(next()); } 48 | double nextDouble() { return Double.parseDouble(next()); } 49 | String nextLine() { 50 | String str = ""; 51 | try { str = br.readLine(); } 52 | catch (IOException e) { e.printStackTrace(); } 53 | return str; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /1697.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws IOException { 7 | FastReader fr = new FastReader(); 8 | 9 | int n = fr.nextInt(); 10 | int k = fr.nextInt(); 11 | 12 | boolean[] visited = new boolean[100001]; 13 | int[] dist = new int[100001]; 14 | 15 | // BFS 수행 16 | Queue queue = new LinkedList<>(); 17 | queue.offer(n); 18 | visited[n] = true; 19 | while (!queue.isEmpty()) { 20 | int cur = queue.poll(); 21 | // System.out.println(cur); 22 | if (cur + 1 <= 100000) { 23 | if (!visited[cur + 1]) { 24 | dist[cur + 1] = dist[cur] + 1; 25 | queue.offer(cur + 1); 26 | visited[cur + 1] = true; 27 | } 28 | } 29 | if (cur - 1 >= 0) { 30 | if (!visited[cur - 1]) { 31 | dist[cur - 1] = dist[cur] + 1; 32 | queue.offer(cur - 1); 33 | visited[cur - 1] = true; 34 | } 35 | } 36 | if (cur * 2 <= 100000) { 37 | if (!visited[cur * 2]) { 38 | dist[cur * 2] = dist[cur] + 1; 39 | queue.offer(cur * 2); 40 | visited[cur * 2] = true; 41 | } 42 | } 43 | } 44 | System.out.println(dist[k]); 45 | } 46 | 47 | public static class FastReader { 48 | BufferedReader br; 49 | StringTokenizer st; 50 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 51 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 52 | String next() { 53 | while (st == null || !st.hasMoreElements()) { 54 | try { st = new StringTokenizer(br.readLine()); } 55 | catch (IOException e) { e.printStackTrace(); } 56 | } 57 | return st.nextToken(); 58 | } 59 | int nextInt() { return Integer.parseInt(next()); } 60 | long nextLong() { return Long.parseLong(next()); } 61 | double nextDouble() { return Double.parseDouble(next()); } 62 | String nextLine() { 63 | String str = ""; 64 | try { str = br.readLine(); } 65 | catch (IOException e) { e.printStackTrace(); } 66 | return str; 67 | } 68 | } 69 | } 70 | 71 | /* 72 | import java.io.*; 73 | import java.util.*; 74 | 75 | public class Main { 76 | 77 | public static int[] visited = new int[100001]; 78 | public static int n, k; 79 | 80 | public static void main(String[] args) throws IOException { 81 | Scanner scanner = new Scanner(System.in); 82 | 83 | n = scanner.nextInt(); 84 | k = scanner.nextInt(); 85 | 86 | Queue q = new LinkedList<>(); 87 | visited[n] = 1; 88 | q.offer(n); 89 | while (!q.isEmpty()) { 90 | int current = q.poll(); 91 | if (current - 1 >= 0) { 92 | if (visited[current - 1] == 0) { 93 | q.offer(current - 1); 94 | visited[current - 1] = visited[current] + 1; 95 | } 96 | } 97 | if (current + 1 <= 100000) { 98 | if (visited[current + 1] == 0) { 99 | q.offer(current + 1); 100 | visited[current + 1] = visited[current] + 1; 101 | } 102 | } 103 | if (current * 2 <= 100000) { 104 | if (visited[current * 2] == 0) { 105 | q.offer(current * 2); 106 | visited[current * 2] = visited[current] + 1; 107 | } 108 | } 109 | } 110 | 111 | System.out.println(visited[k] - 1); 112 | } 113 | } 114 | */ 115 | -------------------------------------------------------------------------------- /1697.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | 3 | n, k = map(int, input().split()) 4 | visited = [0] * 100001 5 | 6 | queue = deque() 7 | queue.append(n) 8 | visited[n] = 1 9 | 10 | while queue: 11 | current = queue.popleft() 12 | if current - 1 >= 0: 13 | if visited[current - 1] == 0: 14 | visited[current - 1] = visited[current] + 1 15 | queue.append(current - 1) 16 | if current + 1 <= 100000: 17 | if visited[current + 1] == 0: 18 | visited[current + 1] = visited[current] + 1 19 | queue.append(current + 1) 20 | if current * 2 <= 100000: 21 | if visited[current * 2] == 0: 22 | visited[current * 2] = visited[current] + 1 23 | queue.append(current * 2) 24 | 25 | print(visited[k] - 1) 26 | -------------------------------------------------------------------------------- /18108.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | int year = scanner.nextInt(); // 불기 연도가 입력되었을 때 7 | System.out.println(year - 543); // 서기 연도를 출력 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /18312.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws IOException { 7 | FastReader fr = new FastReader(); 8 | int h = fr.nextInt(); 9 | int t = fr.nextInt(); 10 | int cnt = 0; 11 | 12 | for (int i = 0; i <= h; i++) { 13 | for (int j = 0; j < 60; j++) { 14 | for (int k = 0; k < 60; k++) { 15 | if (i % 10 == t || // 시(hour)의 일의 자릿수가 K인 경우 16 | i / 10 == t || // 시(hour)의 십의 자릿수가 K인 경우 17 | j % 10 == t || // 분(minute)의 일의 자릿수가 K인 경우 18 | j / 10 == t || // 분(minute)의 십의 자릿수가 K인 경우 19 | k % 10 == t || // 초(second)의 일의 자릿수가 K인 경우 20 | k / 10 == t) // 초(second)의 십의 자릿수가 K인 경우 21 | cnt++; 22 | } 23 | } 24 | } 25 | System.out.println(cnt); 26 | } 27 | 28 | public static class FastReader { 29 | BufferedReader br; 30 | StringTokenizer st; 31 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 32 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 33 | String next() { 34 | while (st == null || !st.hasMoreElements()) { 35 | try { st = new StringTokenizer(br.readLine()); } 36 | catch (IOException e) { e.printStackTrace(); } 37 | } 38 | return st.nextToken(); 39 | } 40 | int nextInt() { return Integer.parseInt(next()); } 41 | long nextLong() { return Long.parseLong(next()); } 42 | double nextDouble() { return Double.parseDouble(next()); } 43 | String nextLine() { 44 | String str = ""; 45 | try { str = br.readLine(); } 46 | catch (IOException e) { e.printStackTrace(); } 47 | return str; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /18870.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String args[]) throws Exception { 7 | FastReader fr = new FastReader(); 8 | int n = fr.nextInt(); // 데이터 개수 9 | ArrayList arr = new ArrayList<>(); 10 | for (int i = 0; i < n; i++) 11 | arr.add(fr.nextInt()); 12 | HashSet set = new HashSet<>(); 13 | for (int x : arr) set.add(x); 14 | ArrayList unique = new ArrayList<>(set); 15 | Collections.sort(unique); 16 | 17 | HashMap map = new HashMap<>(); 18 | for (int i = 0; i < unique.size(); i++) { 19 | map.put(unique.get(i), i); 20 | } 21 | 22 | StringWriter stringWriter = new StringWriter(); 23 | BufferedWriter buffWriter = new BufferedWriter(stringWriter); 24 | for (int x : arr) buffWriter.write(map.get(x) + " "); 25 | buffWriter.flush(); 26 | System.out.println(stringWriter.getBuffer()); 27 | } 28 | 29 | public static class FastReader { 30 | BufferedReader br; 31 | StringTokenizer st; 32 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 33 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 34 | String next() { 35 | while (st == null || !st.hasMoreElements()) { 36 | try { st = new StringTokenizer(br.readLine()); } 37 | catch (IOException e) { e.printStackTrace(); } 38 | } 39 | return st.nextToken(); 40 | } 41 | int nextInt() { return Integer.parseInt(next()); } 42 | long nextLong() { return Long.parseLong(next()); } 43 | double nextDouble() { return Double.parseDouble(next()); } 44 | String nextLine() { 45 | String str = ""; 46 | try { str = br.readLine(); } 47 | catch (IOException e) { e.printStackTrace(); } 48 | return str; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /1920.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int lowerBound(int[] arr, int target, int start, int end) { 7 | while (start < end) { 8 | int mid = (start + end) / 2; 9 | if (arr[mid] >= target) end = mid; 10 | else start = mid + 1; 11 | } 12 | return end; 13 | } 14 | 15 | public static int upperBound(int[] arr, int target, int start, int end) { 16 | while (start < end) { 17 | int mid = (start + end) / 2; 18 | if (arr[mid] > target) end = mid; 19 | else start = mid + 1; 20 | } 21 | return end; 22 | } 23 | 24 | // 값이 [left_value, right_value]인 데이터의 개수를 반환하는 함수 25 | public static int countByRange(int[] arr, int leftValue, int rightValue) { 26 | // 유의: lowerBound와 upperBound는 end 변수의 값을 배열의 길이로 설정 27 | int rightIndex = upperBound(arr, rightValue, 0, arr.length); 28 | int leftIndex = lowerBound(arr, leftValue, 0, arr.length); 29 | return rightIndex - leftIndex; 30 | } 31 | 32 | public static void main(String[] args) { 33 | FastReader fr = new FastReader(); 34 | 35 | // n개의 데이터가 들어갈 배열 36 | int n = fr.nextInt(); 37 | int[] arr = new int[n]; 38 | 39 | // n개의 데이터 입력받기 40 | for (int i = 0; i < n; i++) { 41 | arr[i] = fr.nextInt(); 42 | } 43 | Arrays.sort(arr); // 오름차순 정렬 44 | 45 | // m개의 쿼리 처리 46 | int m = fr.nextInt(); 47 | for (int i = 0; i < m; i++) { 48 | int x = fr.nextInt(); 49 | int result = countByRange(arr, x, x); 50 | if (result >= 1) System.out.println(1); 51 | else System.out.println(0); 52 | } 53 | } 54 | 55 | public static class FastReader { 56 | BufferedReader br; 57 | StringTokenizer st; 58 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 59 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 60 | String next() { 61 | while (st == null || !st.hasMoreElements()) { 62 | try { st = new StringTokenizer(br.readLine()); } 63 | catch (IOException e) { e.printStackTrace(); } 64 | } 65 | return st.nextToken(); 66 | } 67 | int nextInt() { return Integer.parseInt(next()); } 68 | long nextLong() { return Long.parseLong(next()); } 69 | double nextDouble() { return Double.parseDouble(next()); } 70 | String nextLine() { 71 | String str = ""; 72 | try { str = br.readLine(); } 73 | catch (IOException e) { e.printStackTrace(); } 74 | return str; 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /1946.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | /* 5 | - 다른 모든 지원자와 비교했을 때 (1) 서류 성적과 (2) 면접 성적 중 적어도 하나가, 다른 지원자보다 떨어지지 않는 사람의 수를 세는 문제다. 6 | (3, 6) 7 | (7, 3) 8 | (4, 2) 9 | (1, 4) 10 | (5, 7) 11 | (2, 5) 12 | (6, 1) 13 | - X를 기준으로 오름차순 정렬 14 | - 위에서부터 한 명씩 보면서, minY보다 순위가 높다면 카운트 15 | (1, 4) count: 1, minY: 4 16 | (2, 5) minY: 4 17 | (3, 6) minY: 4 18 | (4, 2) count: 2, minY: 2 19 | (5, 7) minY: 2 20 | (6, 1) count: 3, minY: 1 21 | (7, 3) minY: 1 22 | */ 23 | 24 | class Point implements Comparable { 25 | public int x; 26 | public int y; 27 | public Point (int x, int y) { 28 | this.x = x; 29 | this.y = y; 30 | } 31 | @Override 32 | public int compareTo(Point other) { 33 | return this.x - other.x; 34 | } 35 | } 36 | 37 | public class Main { 38 | public static void main(String[] args) { 39 | FastReader fr = new FastReader(); 40 | int testCase = fr.nextInt(); 41 | for (int tc = 0; tc < testCase; tc++) { 42 | int n = fr.nextInt(); 43 | ArrayList arr = new ArrayList(); 44 | for (int i = 0; i < n; i++) { 45 | int x = fr.nextInt(); 46 | int y = fr.nextInt(); 47 | arr.add(new Point(x, y)); 48 | } 49 | Collections.sort(arr); 50 | int cnt = 0; 51 | int minY = 100001; 52 | for (Point point : arr) { 53 | if (point.y < minY) { 54 | minY = point.y; 55 | cnt++; 56 | } 57 | } 58 | System.out.println(cnt); 59 | } 60 | } 61 | 62 | public static class FastReader { 63 | BufferedReader br; 64 | StringTokenizer st; 65 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 66 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 67 | String next() { 68 | while (st == null || !st.hasMoreElements()) { 69 | try { st = new StringTokenizer(br.readLine()); } 70 | catch (IOException e) { e.printStackTrace(); } 71 | } 72 | return st.nextToken(); 73 | } 74 | int nextInt() { return Integer.parseInt(next()); } 75 | long nextLong() { return Long.parseLong(next()); } 76 | double nextDouble() { return Double.parseDouble(next()); } 77 | String nextLine() { 78 | String str = ""; 79 | try { str = br.readLine(); } 80 | catch (IOException e) { e.printStackTrace(); } 81 | return str; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /2178.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class Node { 5 | public int x; 6 | public int y; 7 | 8 | public Node(int x, int y) { 9 | this.x = x; 10 | this.y = y; 11 | } 12 | } 13 | 14 | public class Main { 15 | 16 | public static void main(String[] args) throws IOException { 17 | FastReader fr = new FastReader(); 18 | 19 | int n = fr.nextInt(); 20 | int m = fr.nextInt(); 21 | 22 | // (N X M) 크기의 배열 초기화 23 | int[][] graph = new int[n][m]; 24 | boolean[][] visited = new boolean[n][m]; 25 | 26 | // 2차원 리스트의 맵 정보 입력 받기 27 | for (int i = 0; i < n; i++) { 28 | String str = fr.nextLine(); 29 | for (int j = 0; j < m; j++) { 30 | graph[i][j] = str.charAt(j) - '0'; 31 | } 32 | } 33 | 34 | // 좌, 하, 상, 우 35 | int[] dx = {0, 1, -1, 0}; 36 | int[] dy = {-1, 0, 0, 1}; 37 | 38 | // BFS 수행 39 | Queue queue = new LinkedList<>(); 40 | queue.offer(new Node(0, 0)); 41 | visited[0][0] = true; 42 | while (!queue.isEmpty()) { // 큐가 빌 때까지 반복 43 | Node cur = queue.poll(); // 매번 큐에서 원소를 꺼내고 44 | int x = cur.x; 45 | int y = cur.y; 46 | for (int i = 0; i < 4; i++) { // 꺼낸 원소와 인접한 원소들 방문 47 | int nx = x + dx[i]; 48 | int ny = y + dy[i]; 49 | // 인접한 원소에 방문 불가능하다면 무시 50 | if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; 51 | if (visited[nx][ny]) continue; 52 | if (graph[nx][ny] == 0) continue; 53 | // 방문이 가능하다면 54 | visited[nx][ny] = true; 55 | graph[nx][ny] = graph[x][y] + 1; 56 | queue.offer(new Node(nx, ny)); 57 | } 58 | } 59 | System.out.println(graph[n - 1][m - 1]); 60 | } 61 | 62 | public static class FastReader { 63 | BufferedReader br; 64 | StringTokenizer st; 65 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 66 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 67 | String next() { 68 | while (st == null || !st.hasMoreElements()) { 69 | try { st = new StringTokenizer(br.readLine()); } 70 | catch (IOException e) { e.printStackTrace(); } 71 | } 72 | return st.nextToken(); 73 | } 74 | int nextInt() { return Integer.parseInt(next()); } 75 | long nextLong() { return Long.parseLong(next()); } 76 | double nextDouble() { return Double.parseDouble(next()); } 77 | String nextLine() { 78 | String str = ""; 79 | try { str = br.readLine(); } 80 | catch (IOException e) { e.printStackTrace(); } 81 | return str; 82 | } 83 | } 84 | } 85 | 86 | /* 87 | import java.io.*; 88 | import java.util.*; 89 | 90 | class Node { 91 | 92 | private int x; 93 | private int y; 94 | 95 | public Node(int x, int y) { 96 | this.x = x; 97 | this.y = y; 98 | } 99 | 100 | public int getX() { 101 | return this.x; 102 | } 103 | 104 | public int getY() { 105 | return this.y; 106 | } 107 | } 108 | 109 | public class Main { 110 | 111 | public static int[][] visited = new int[100][100]; 112 | public static int[][] arr = new int[100][100]; 113 | public static int n, m; 114 | public static int[] dx = { 0, -1, 0, 1 }; 115 | public static int[] dy = { 1, 0, -1, 0 }; 116 | 117 | public static void main(String[] args) throws IOException { 118 | Scanner scanner = new Scanner(System.in); 119 | 120 | n = scanner.nextInt(); 121 | m = scanner.nextInt(); 122 | 123 | for (int i = 0; i < n; i++) { 124 | String line = scanner.next(); 125 | for (int j = 0; j < line.length(); j++) { 126 | int current = line.charAt(j) - '0'; 127 | arr[i][j] = current; 128 | } 129 | } 130 | 131 | Queue q = new LinkedList<>(); 132 | visited[0][0] = 1; 133 | q.offer(new Node(0, 0)); 134 | 135 | while (!q.isEmpty()) { 136 | Node cur = q.poll(); 137 | int x = cur.getX(); 138 | int y = cur.getY(); 139 | for (int i = 0; i < 4; i++) { 140 | int nx = x + dx[i]; 141 | int ny = y + dy[i]; 142 | if (nx < 0 || nx >= n || ny < 0 || ny >= m) 143 | continue; 144 | if (arr[nx][ny] == 0) 145 | continue; 146 | if (visited[nx][ny] != 0) 147 | continue; 148 | visited[nx][ny] = visited[x][y] + 1; 149 | q.offer(new Node(nx, ny)); 150 | } 151 | } 152 | System.out.println(visited[n - 1][m - 1]); 153 | } 154 | } 155 | */ 156 | -------------------------------------------------------------------------------- /2438.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); 7 | int n = fr.nextInt(); 8 | for (int i = 1; i <= n; i++) { // 한 줄씩 9 | for (int j = 0; j < i; j++) { // 각 줄에서 i개의 별 출력 10 | System.out.print("*"); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | public static class FastReader { 16 | BufferedReader br; 17 | StringTokenizer st; 18 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 19 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 20 | String next() { 21 | while (st == null || !st.hasMoreElements()) { 22 | try { st = new StringTokenizer(br.readLine()); } 23 | catch (IOException e) { e.printStackTrace(); } 24 | } 25 | return st.nextToken(); 26 | } 27 | int nextInt() { return Integer.parseInt(next()); } 28 | long nextLong() { return Long.parseLong(next()); } 29 | double nextDouble() { return Double.parseDouble(next()); } 30 | String nextLine() { 31 | String str = ""; 32 | try { str = br.readLine(); } 33 | catch (IOException e) { e.printStackTrace(); } 34 | return str; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /2439.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | int n = fr.nextInt(); 8 | // 1층부터 N층까지 반복 9 | for (int i = 0; i < n; i++) { 10 | // 먼저 띄어쓰기(space)를 출력 11 | for (int j = 0; j < n - i - 1; j++) { 12 | System.out.print(" "); 13 | } 14 | // 별(*)을 출력 15 | for (int j = 0; j < i + 1; j++) { 16 | System.out.print("*"); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | public static class FastReader { 22 | BufferedReader br; 23 | StringTokenizer st; 24 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 25 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 26 | String next() { 27 | while (st == null || !st.hasMoreElements()) { 28 | try { st = new StringTokenizer(br.readLine()); } 29 | catch (IOException e) { e.printStackTrace(); } 30 | } 31 | return st.nextToken(); 32 | } 33 | int nextInt() { return Integer.parseInt(next()); } 34 | long nextLong() { return Long.parseLong(next()); } 35 | double nextDouble() { return Double.parseDouble(next()); } 36 | String nextLine() { 37 | String str = ""; 38 | try { str = br.readLine(); } 39 | catch (IOException e) { e.printStackTrace(); } 40 | return str; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /2525.java: -------------------------------------------------------------------------------- 1 | /* 2 | 입력: A시 B분 3 | 걸리는 시간: C분 4 | 5 | [푸는 방법] 6 | 1) 입력을 그냥 "분"으로만 바꾸기 7 | 2) "분"이 주어졌을 때, 시각을 알려주는 함수를 짜기 8 | 9 | [예시] 10 | 입력: 17시 40분 + 80분 => 1060분 + 80분 => 1140분 11 | 12 | 1. 1140를 1440으로 나눈 나머지를 취하기: 총 1140분 13 | 2. 60으로 나눈 몫을 시로 취하기: 19시 14 | 3. 60으로 나눈 나머지를 분으로 취하기: 0분 15 | */ 16 | 17 | import java.util.*; 18 | 19 | class Main { 20 | public static void main(String[] args) { 21 | Scanner scanner = new Scanner(System.in); 22 | int a = scanner.nextInt(); 23 | int b = scanner.nextInt(); 24 | int c = scanner.nextInt(); 25 | 26 | int totalMinute = a * 60 + b + c; 27 | totalMinute %= 1440; 28 | int hour = totalMinute / 60; 29 | int minute = totalMinute % 60; 30 | 31 | System.out.println(hour + " " + minute); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /2529.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static boolean[] visited = new boolean[10]; 7 | public static Stack result = new Stack(); 8 | public static ArrayList arr = new ArrayList(); 9 | public static int k; 10 | public static String current; 11 | public static String first = ""; 12 | 13 | public static void dfs(int depth) { 14 | // 각 순열에 대한 처리 15 | if (depth == k + 1) { 16 | // 부등식을 만족하는지 확인 17 | boolean check = true; 18 | for (int i = 0; i < k; i++) { 19 | if (arr.get(i).equals("<")) { 20 | if (result.get(i) > result.get(i + 1)) 21 | check = false; 22 | } else if (arr.get(i).equals(">")) { 23 | if (result.get(i) < result.get(i + 1)) 24 | check = false; 25 | } 26 | } 27 | // 부등식을 만족하는 경우에 28 | if (check) { 29 | current = ""; 30 | for (int x : result) { 31 | current += x + ""; 32 | } 33 | // 첫째 문자열은 "가장 작은 수" 34 | if (first.equals("")) 35 | first = current; 36 | } 37 | return; 38 | } 39 | // 0부터 9까지의 숫자를 하나씩 고르는 순열(백트래킹) 40 | for (int i = 0; i < 10; i++) { 41 | // 미리 고른 숫자라면 무시하도록 42 | if (visited[i]) 43 | continue; 44 | // 현재 선택한 숫자 방문 처리 45 | visited[i] = true; 46 | result.push(i); 47 | dfs(depth + 1); // 재귀 함수 호출 48 | // 현재 선택한 숫자 방문 처리 취소 49 | visited[i] = false; 50 | result.pop(); 51 | } 52 | } 53 | 54 | public static void main(String[] args) throws IOException { 55 | Scanner scanner = new Scanner(System.in); 56 | k = scanner.nextInt(); 57 | for (int i = 0; i < k; i++) { 58 | arr.add(scanner.next()); 59 | } 60 | dfs(0); 61 | // 마지막에 남은 current 값은 "가장 큰 문자열" 62 | System.out.println(current + "\n" + first); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /2562.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); 7 | int n = 9; 8 | ArrayList arrayList = new ArrayList<>(); 9 | // n개의 데이터를 하나씩 입력받기 10 | for (int i = 0; i < n; i++) { 11 | int x = fr.nextInt(); 12 | arrayList.add(x); 13 | } 14 | int maxValue = (int) -1e9; // 가장 큰 값 15 | int maxIndex = 0; // 가장 큰 값의 위치 16 | for (int i = 0; i < n; i++) { 17 | if (maxValue < arrayList.get(i)) { 18 | maxValue = arrayList.get(i); 19 | maxIndex = i; 20 | } 21 | } 22 | System.out.println(maxValue); 23 | System.out.println(maxIndex + 1); 24 | } 25 | public static class FastReader { 26 | BufferedReader br; 27 | StringTokenizer st; 28 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 29 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 30 | String next() { 31 | while (st == null || !st.hasMoreElements()) { 32 | try { st = new StringTokenizer(br.readLine()); } 33 | catch (IOException e) { e.printStackTrace(); } 34 | } 35 | return st.nextToken(); 36 | } 37 | int nextInt() { return Integer.parseInt(next()); } 38 | long nextLong() { return Long.parseLong(next()); } 39 | double nextDouble() { return Double.parseDouble(next()); } 40 | String nextLine() { 41 | String str = ""; 42 | try { str = br.readLine(); } 43 | catch (IOException e) { e.printStackTrace(); } 44 | return str; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /2562_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | int n = 9; 8 | ArrayList arrayList = new ArrayList(); 9 | for (int i = 0; i < n; i++) { 10 | int x = fr.nextInt(); 11 | arrayList.add(x); 12 | } 13 | /* 14 | // 푸는 방법 1 15 | int maxValue = 0; 16 | int maxIndex = -1; 17 | for (int i = 0; i < n; i++) { 18 | int x = arrayList.get(i); 19 | if (x > maxValue) { 20 | maxValue = x; 21 | maxIndex = i; 22 | } 23 | } 24 | */ 25 | int maxValue = Collections.max(arrayList); 26 | int maxIndex = -1; 27 | for (int i = 0; i < n; i++) { 28 | int x = arrayList.get(i); 29 | if (x == maxValue) { 30 | maxIndex = i; 31 | break; 32 | } 33 | } 34 | System.out.println(maxValue + "\n" + (maxIndex + 1)); 35 | } 36 | public static class FastReader { 37 | BufferedReader br; 38 | StringTokenizer st; 39 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 40 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 41 | String next() { 42 | while (st == null || !st.hasMoreElements()) { 43 | try { st = new StringTokenizer(br.readLine()); } 44 | catch (IOException e) { e.printStackTrace(); } 45 | } 46 | return st.nextToken(); 47 | } 48 | int nextInt() { return Integer.parseInt(next()); } 49 | long nextLong() { return Long.parseLong(next()); } 50 | double nextDouble() { return Double.parseDouble(next()); } 51 | String nextLine() { 52 | String str = ""; 53 | try { str = br.readLine(); } 54 | catch (IOException e) { e.printStackTrace(); } 55 | return str; 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /2577.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); 7 | int a = fr.nextInt(); // 150 8 | int b = fr.nextInt(); // 266 9 | int c = fr.nextInt(); // 427 10 | int result = a * b * c; // 17037300 11 | // "17037300" 12 | String temp = Integer.toString(result); 13 | int[] cnt = new int[10]; // 0~9까지의 숫자 개수 14 | for (int i = 0; i < temp.length(); i++) { 15 | cnt[Integer.parseInt(temp.charAt(i) + "")] += 1; 16 | } 17 | for (int i = 0; i < 10; i++) System.out.println(cnt[i]); 18 | } 19 | public static class FastReader { 20 | BufferedReader br; 21 | StringTokenizer st; 22 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 23 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 24 | String next() { 25 | while (st == null || !st.hasMoreElements()) { 26 | try { st = new StringTokenizer(br.readLine()); } 27 | catch (IOException e) { e.printStackTrace(); } 28 | } 29 | return st.nextToken(); 30 | } 31 | int nextInt() { return Integer.parseInt(next()); } 32 | long nextLong() { return Long.parseLong(next()); } 33 | double nextDouble() { return Double.parseDouble(next()); } 34 | String nextLine() { 35 | String str = ""; 36 | try { str = br.readLine(); } 37 | catch (IOException e) { e.printStackTrace(); } 38 | return str; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /2579.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int dp[][] = new int[300][2]; 7 | public static int arr[] = new int[300]; 8 | 9 | public static void main(String[] args) { 10 | FastReader fr = new FastReader(); 11 | 12 | int n = fr.nextInt(); 13 | for (int i = 0; i < n; i++) { 14 | arr[i] = fr.nextInt(); 15 | } 16 | 17 | // d[i][0]: 1개만 연속으로 밟은 경우 18 | // d[i][1]: 이미 2개 연속으로 밟은 경우 19 | dp[0][0] = arr[0]; 20 | dp[0][1] = arr[0]; 21 | dp[1][0] = arr[1]; 22 | dp[1][1] = arr[0] + arr[1]; 23 | for (int i = 2; i < n; i++) { 24 | dp[i][0] = Math.max(dp[i - 2][0], dp[i - 2][1]) + arr[i]; 25 | dp[i][1] = dp[i - 1][0] + arr[i]; 26 | } 27 | System.out.println(Math.max(dp[n - 1][0], dp[n - 1][1])); 28 | } 29 | public static class FastReader { 30 | BufferedReader br; 31 | StringTokenizer st; 32 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 33 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 34 | String next() { 35 | while (st == null || !st.hasMoreElements()) { 36 | try { st = new StringTokenizer(br.readLine()); } 37 | catch (IOException e) { e.printStackTrace(); } 38 | } 39 | return st.nextToken(); 40 | } 41 | int nextInt() { return Integer.parseInt(next()); } 42 | long nextLong() { return Long.parseLong(next()); } 43 | double nextDouble() { return Double.parseDouble(next()); } 44 | String nextLine() { 45 | String str = ""; 46 | try { str = br.readLine(); } 47 | catch (IOException e) { e.printStackTrace(); } 48 | return str; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /2588.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | Scanner scanner = new Scanner(System.in); 6 | int a = scanner.nextInt(); 7 | int b = scanner.nextInt(); 8 | 9 | // b = 385라면, 10 | int x_1 = b % 10; // 일의 자릿수: 5 11 | int x_2 = (b / 10) % 10; // 십의 자릿수: 8 12 | int x_3 = (b / 100); // 백의 자릿수: 3 13 | 14 | System.out.println(a * x_1); 15 | System.out.println(a * x_2); 16 | System.out.println(a * x_3); 17 | System.out.println(a * b); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /2606.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int result = 0; 7 | public static boolean[] visited = new boolean[101]; 8 | public static ArrayList> graph = new ArrayList>(); 9 | 10 | public static void dfs(int x) { 11 | for (int i = 0; i < graph.get(x).size(); i++) { 12 | int y = graph.get(x).get(i); 13 | if (!visited[y]) { 14 | visited[y] = true; 15 | dfs(y); 16 | result++; 17 | } 18 | } 19 | } 20 | 21 | public static void main(String[] args) throws IOException { 22 | FastReader fr = new FastReader(); 23 | 24 | int n = fr.nextInt(); // 노드의 개수 25 | for (int i = 0; i <= n; i++) { // 각 노드와 연결된 인접 노드를 담기 26 | graph.add(new ArrayList()); // 0번 노드부터 n번 노드까지 27 | } 28 | int m = fr.nextInt(); // 간선의 개수 29 | for (int i = 0; i < m; i++) { // (x, y) => x와 y가 서로 연결 30 | int x = fr.nextInt(); 31 | int y = fr.nextInt(); 32 | graph.get(x).add(y); 33 | graph.get(y).add(x); 34 | } 35 | 36 | visited[1] = true; 37 | dfs(1); 38 | System.out.println(result); 39 | } 40 | 41 | public static class FastReader { 42 | BufferedReader br; 43 | StringTokenizer st; 44 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 45 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 46 | String next() { 47 | while (st == null || !st.hasMoreElements()) { 48 | try { st = new StringTokenizer(br.readLine()); } 49 | catch (IOException e) { e.printStackTrace(); } 50 | } 51 | return st.nextToken(); 52 | } 53 | int nextInt() { return Integer.parseInt(next()); } 54 | long nextLong() { return Long.parseLong(next()); } 55 | double nextDouble() { return Double.parseDouble(next()); } 56 | String nextLine() { 57 | String str = ""; 58 | try { str = br.readLine(); } 59 | catch (IOException e) { e.printStackTrace(); } 60 | return str; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /2665.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | # 일반적인 큐(queue) 대신에 힙(heap)을 쓰는 이유는 3 | # (검은 방을 없애는 횟수, x, y) 형태로 원소를 입력 받으면 4 | # 검은 방을 없애는 횟수가 적은 순서대로 꺼내지기 때문에 5 | # 목표 지점에 처음 도달한 경우, 가장 검은 방은 적게 없앤 결과가 나옴. 6 | 7 | n = int(input()) 8 | # 2차원 맵 생성 9 | graph = [[0] * n for _ in range(n)] 10 | # 2차원 맵 입력받기 11 | for i in range(n): 12 | row = input() 13 | for j in range(n): 14 | graph[i][j] = int(row[j]) 15 | 16 | # 상, 하, 좌, 우 방향 정보 17 | dx = [-1, 1, 0, 0] 18 | dy = [0, 0, -1, 1] 19 | 20 | # 방문 처리 배열 21 | visited = [[-1] * n for _ in range(n)] 22 | 23 | # BFS 수행 24 | queue = [] 25 | # (검은 방을 없앤 횟수, x, y)를 큐에 삽입 26 | heapq.heappush(queue, (0, 0, 0)) 27 | visited[0][0] = 0 # 검은 방을 없앤 횟수 28 | while queue: # 큐가 빌 때까지 29 | count, x, y = heapq.heappop(queue) 30 | for i in range(4): 31 | nx = x + dx[i] 32 | ny = y + dy[i] 33 | # 맵을 벗어나는 경우 무시 34 | if nx < 0 or nx >= n or ny < 0 or ny >= n: 35 | continue 36 | # 방문하지 않은 위치라면 37 | if visited[nx][ny] == -1: 38 | # 검은 방인 경우 카운트를 증가해서 삽입 39 | if graph[nx][ny] == 0: 40 | visited[nx][ny] = count + 1 41 | heapq.heappush(queue, (count + 1, nx, ny)) 42 | # 흰 방인 경우 카운트를 그대로 삽입 43 | else: 44 | visited[nx][ny] = count 45 | heapq.heappush(queue, (count, nx, ny)) 46 | 47 | print(visited[n - 1][n - 1]) 48 | -------------------------------------------------------------------------------- /2675.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | int t = fr.nextInt(); 8 | for (int testCase = 0; testCase < t; testCase++) { 9 | int r = fr.nextInt(); // 반복 횟수 r 10 | String s = fr.next(); // 문자열 s 11 | String result = ""; 12 | // 문자열 s에 포함된 각 문자를 하나씩 확인하며 13 | for (int i = 0; i < s.length(); i++) { 14 | // 해당 문자를 r번만큼 반복하기 15 | char x = s.charAt(i); 16 | for (int j = 0; j < r; j++) { 17 | result += Character.toString(x); 18 | } 19 | } 20 | System.out.println(result); 21 | } 22 | } 23 | public static class FastReader { 24 | BufferedReader br; 25 | StringTokenizer st; 26 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 27 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 28 | String next() { 29 | while (st == null || !st.hasMoreElements()) { 30 | try { st = new StringTokenizer(br.readLine()); } 31 | catch (IOException e) { e.printStackTrace(); } 32 | } 33 | return st.nextToken(); 34 | } 35 | int nextInt() { return Integer.parseInt(next()); } 36 | long nextLong() { return Long.parseLong(next()); } 37 | double nextDouble() { return Double.parseDouble(next()); } 38 | String nextLine() { 39 | String str = ""; 40 | try { str = br.readLine(); } 41 | catch (IOException e) { e.printStackTrace(); } 42 | return str; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /2739.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); 7 | int n = fr.nextInt(); 8 | for (int i = 1; i <= 9; i++) { 9 | System.out.println(n + " * " + i + " = " + (n * i)); 10 | } 11 | } 12 | public static class FastReader { 13 | BufferedReader br; 14 | StringTokenizer st; 15 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 16 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 17 | String next() { 18 | while (st == null || !st.hasMoreElements()) { 19 | try { st = new StringTokenizer(br.readLine()); } 20 | catch (IOException e) { e.printStackTrace(); } 21 | } 22 | return st.nextToken(); 23 | } 24 | int nextInt() { return Integer.parseInt(next()); } 25 | long nextLong() { return Long.parseLong(next()); } 26 | double nextDouble() { return Double.parseDouble(next()); } 27 | String nextLine() { 28 | String str = ""; 29 | try { str = br.readLine(); } 30 | catch (IOException e) { e.printStackTrace(); } 31 | return str; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /2750.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | 8 | int n = fr.nextInt(); 9 | ArrayList arrayList = new ArrayList<>(); 10 | for (int i = 0; i < n; i++) { 11 | int x = fr.nextInt(); 12 | arrayList.add(x); 13 | } 14 | 15 | Collections.sort(arrayList); 16 | for (int i = 0; i < n; i++) { 17 | System.out.println(arrayList.get(i)); 18 | } 19 | } 20 | 21 | public static class FastReader { 22 | BufferedReader br; 23 | StringTokenizer st; 24 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 25 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 26 | String next() { 27 | while (st == null || !st.hasMoreElements()) { 28 | try { st = new StringTokenizer(br.readLine()); } 29 | catch (IOException e) { e.printStackTrace(); } 30 | } 31 | return st.nextToken(); 32 | } 33 | int nextInt() { return Integer.parseInt(next()); } 34 | long nextLong() { return Long.parseLong(next()); } 35 | double nextDouble() { return Double.parseDouble(next()); } 36 | String nextLine() { 37 | String str = ""; 38 | try { str = br.readLine(); } 39 | catch (IOException e) { e.printStackTrace(); } 40 | return str; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /2751.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | 8 | int n = fr.nextInt(); 9 | ArrayList arrayList = new ArrayList<>(); 10 | for (int i = 0; i < n; i++) { 11 | int x = fr.nextInt(); 12 | arrayList.add(x); 13 | } 14 | 15 | StringWriter stringWriter = new StringWriter(); 16 | BufferedWriter buffWriter = new BufferedWriter(stringWriter); 17 | 18 | Collections.sort(arrayList); 19 | for (int i = 0; i < n; i++) { 20 | buffWriter.write(arrayList.get(i) + "\n"); 21 | } 22 | // Flush the buffer writer 23 | buffWriter.flush(); 24 | System.out.println(stringWriter.getBuffer()); 25 | } 26 | 27 | public static class FastReader { 28 | BufferedReader br; 29 | StringTokenizer st; 30 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 31 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 32 | String next() { 33 | while (st == null || !st.hasMoreElements()) { 34 | try { st = new StringTokenizer(br.readLine()); } 35 | catch (IOException e) { e.printStackTrace(); } 36 | } 37 | return st.nextToken(); 38 | } 39 | int nextInt() { return Integer.parseInt(next()); } 40 | long nextLong() { return Long.parseLong(next()); } 41 | double nextDouble() { return Double.parseDouble(next()); } 42 | String nextLine() { 43 | String str = ""; 44 | try { str = br.readLine(); } 45 | catch (IOException e) { e.printStackTrace(); } 46 | return str; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /2805.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static void main(String[] args) throws IOException { 7 | FastReader fr = new FastReader(); 8 | 9 | int n = fr.nextInt(); // 나무의 개수 10 | int m = fr.nextInt(); // 가져갈 최소 나무 크기 11 | 12 | // n개의 나무 길이에 대해서 입력 13 | int maxLength = (int) -1e9; 14 | int[] arr = new int[n]; 15 | for (int i = 0; i < n; i++) { 16 | arr[i] = fr.nextInt(); 17 | maxLength = Math.max(maxLength, arr[i]); 18 | } 19 | 20 | int start = 0; // 탐색 범위 시작점 21 | int end = maxLength; // 탐색 범위 끝점 22 | int result = 0; 23 | while (start <= end) { 24 | int mid = (start + end) / 2; // 중간점(높이) 25 | long total = 0; 26 | // 나무를 하나씩 확인하면서, mid로 자른 뒤에 나무 얻기 27 | for (int i = 0; i < n; i++) { 28 | if (mid < arr[i]) total += arr[i] - mid; 29 | } 30 | if (total < m) { // 더 많이 자르기 31 | end = mid - 1; 32 | } 33 | else { // 덜 자르기 34 | start = mid + 1; 35 | result = mid; 36 | } 37 | } 38 | System.out.println(result); 39 | } 40 | 41 | public static class FastReader { 42 | BufferedReader br; 43 | StringTokenizer st; 44 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 45 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 46 | String next() { 47 | while (st == null || !st.hasMoreElements()) { 48 | try { st = new StringTokenizer(br.readLine()); } 49 | catch (IOException e) { e.printStackTrace(); } 50 | } 51 | return st.nextToken(); 52 | } 53 | int nextInt() { return Integer.parseInt(next()); } 54 | long nextLong() { return Long.parseLong(next()); } 55 | double nextDouble() { return Double.parseDouble(next()); } 56 | String nextLine() { 57 | String str = ""; 58 | try { str = br.readLine(); } 59 | catch (IOException e) { e.printStackTrace(); } 60 | return str; 61 | } 62 | } 63 | } 64 | 65 | /* 66 | 67 | 전체 나무의 개수: 5개 68 | 필요한 나무 높이: 20 69 | 70 | 4 42 40 26 46 71 | 72 | [1단계] 73 | left: 4, right: 46 74 | 이때, mid (height): (4 + 46) / 2 = 25 75 | 즉, 25로 잘랐을 때 얻는 나무의 총 높이는 76 | 17 + 15 + 1 + 21 = 54 77 | 따라서, 충분히 얻었으므로, 높이를 더 높여야 됨. 78 | 79 | [2단계] 80 | left: 26, right: 46 81 | 이때, mid (height): (26 + 46) / 2 = 36 82 | 즉, 36으로 잘랐을 때 얻는 나무의 총 높이는 83 | 6 + 4 + 10 = 20 84 | 따라서, 충분히 얻었으므로, 높이를 더 높여야 됨. 85 | 86 | [3단계] 87 | left: 37, right: 46 88 | ... 89 | 90 | "가장 마지막으로 조건을 만족하는 높이 값"을 출력. 91 | 92 | import java.io.*; 93 | import java.util.*; 94 | 95 | public class Main { 96 | 97 | public static void main(String[] args) throws IOException { 98 | Scanner scanner = new Scanner(System.in); 99 | 100 | // 전체 나무의 개수 입력받기 101 | int n = scanner.nextInt(); 102 | // 필요한 나무의 높이 입력받기 103 | int m = scanner.nextInt(); 104 | 105 | // 각 나무의 높이 입력받기 106 | int[] arr = new int[n]; 107 | for(int i = 0; i < n; i++) { 108 | arr[i] = scanner.nextInt(); 109 | } 110 | 111 | // 이진 탐색 수행 112 | int left = 0; 113 | int right = 1000000000; 114 | int result = 0; 115 | while (left <= right) { 116 | int mid = (left + right) / 2; 117 | // 현재 높이로 자르기 수행 118 | long total = 0; 119 | for (int i = 0; i < n; i++) { 120 | if (mid < arr[i]) total += arr[i] - mid; 121 | } 122 | // 충분히 얻었다면 높이를 더 높여보기 123 | if (total >= m) { 124 | left = mid + 1; 125 | result = mid; 126 | } 127 | else { // 그렇지 않다면, 높이를 낮추어보기 128 | right = mid - 1; 129 | } 130 | } 131 | System.out.println(result); 132 | } 133 | } 134 | 135 | */ 136 | -------------------------------------------------------------------------------- /2884.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); // 문제 해결 소스 코드 7 | int hour = fr.nextInt(); 8 | int minute = fr.nextInt(); 9 | minute -= 45; // 45분 앞당기기 10 | if (minute < 0) { // 분이 음수가 된 경우 11 | minute += 60; 12 | hour -= 1; 13 | if (hour < 0) { 14 | hour += 24; 15 | } 16 | } 17 | System.out.println(hour + " " + minute); 18 | } 19 | public static class FastReader { 20 | BufferedReader br; 21 | StringTokenizer st; 22 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 23 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 24 | String next() { 25 | while (st == null || !st.hasMoreElements()) { 26 | try { st = new StringTokenizer(br.readLine()); } 27 | catch (IOException e) { e.printStackTrace(); } 28 | } 29 | return st.nextToken(); 30 | } 31 | int nextInt() { return Integer.parseInt(next()); } 32 | long nextLong() { return Long.parseLong(next()); } 33 | double nextDouble() { return Double.parseDouble(next()); } 34 | String nextLine() { 35 | String str = ""; 36 | try { str = br.readLine(); } 37 | catch (IOException e) { e.printStackTrace(); } 38 | return str; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /2961.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | public class Main { 5 | 6 | public static boolean[] visited = new boolean[10]; 7 | public static Stack result = new Stack(); 8 | public static int[][] arr = new int[10][2]; 9 | public static int n; 10 | public static int answer = (int) 1e9; 11 | 12 | // N개의 재료 중에서 depth를 선택한 조합 계산 13 | public static void dfs(int depth, int start) { 14 | // "모든 개수"를 고려해야 하므로, depth가 1 이상이면 항상 처리 15 | // 현재 조합에 대한 처리 16 | if (depth >= 1) { 17 | int totalX = 1; 18 | int totalY = 0; 19 | for (int i: result) { 20 | int x = arr[i][0]; 21 | int y = arr[i][01]; 22 | totalX *= x; 23 | totalY += y; 24 | } 25 | answer = Math.min(answer, Math.abs(totalX - totalY)); 26 | } 27 | for (int i = start; i < n; i++) { 28 | if (visited[i]) continue; 29 | // 방문 처리 30 | result.push(i); 31 | visited[i] = true; 32 | dfs(depth + 1, i + 1); // 재귀 함수 호출 33 | // 방문 처리 해제 34 | result.pop(); 35 | visited[i] = false; 36 | } 37 | } 38 | 39 | public static void main(String[] args) throws IOException { 40 | Scanner scanner = new Scanner(System.in); 41 | n = scanner.nextInt(); 42 | for (int i = 0; i < n; i++) { 43 | int x = scanner.nextInt(); 44 | int y = scanner.nextInt(); 45 | arr[i][0] = x; 46 | arr[i][1] = y; 47 | } 48 | dfs(0, 0); 49 | System.out.println(answer); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /2961.py: -------------------------------------------------------------------------------- 1 | """ 2 | * "모든 조합"을 구하는 문제입니다. 3 | * Why? 재료 (A, B, C)를 사용한 것과 재료 (A, C, B)를 사용한 것이 같은 결과를 가진다. 4 | * 이때, 1개만 써도 되고, 2개만 써도 되고, ... N개를 써도 되니까 5 | * "모든 개수"에 대하여 "모든 조합"을 구하는 문제입니다. 6 | 7 | [Example] 8 | * A, B, C, D라는 재료가 있습니다. 9 | * 길이 #1 10 | - A만 사용할 때 11 | - B만 사용할 때 12 | - C만 사용할 때 13 | - D만 사용할 때 14 | * 길이 #2 15 | - (A, B) 16 | - (A, C) 17 | - (A, D) 18 | - (B, C) 19 | - (B, D) 20 | - (C, D) 21 | * 길이 #3 22 | - (A, B, C) 23 | - (A, B, D) 24 | - (A, C, D) 25 | - (B, C, D) 26 | * 길이 #4 27 | - (A, B, C, D) 28 | """ 29 | 30 | import sys 31 | sys.setrecursionlimit(int(1e6)) 32 | 33 | n = int(input()) 34 | arr = [] 35 | for i in range(n): 36 | x, y = map(int, input().split()) 37 | arr.append((x, y)) 38 | 39 | visited = [False] * n 40 | result = [] 41 | answer = int(1e9) 42 | 43 | def dfs(depth, start): 44 | global answer 45 | # 현재 조합에 대하여 결과 계산 46 | if depth >= 1: 47 | totalX = 1; 48 | totalY = 0; 49 | for i in result: 50 | x, y = arr[i] 51 | totalX *= x; 52 | totalY += y; 53 | answer = min(answer, abs(totalX - totalY)) 54 | for i in range(start, n): 55 | if visited[i]: continue 56 | visited[i] = True 57 | result.append(i) 58 | dfs(depth + 1, i + 1) 59 | visited[i] = False 60 | result.pop() 61 | 62 | dfs(0, 0) 63 | print(answer) 64 | -------------------------------------------------------------------------------- /3003.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); // 문제 해결 소스 코드 7 | 8 | int king = fr.nextInt(); 9 | int queen = fr.nextInt(); 10 | int rook = fr.nextInt(); 11 | int bishop = fr.nextInt(); 12 | int knight = fr.nextInt(); 13 | int pawn = fr.nextInt(); 14 | System.out.print((1 - king) + " "); 15 | System.out.print((1 - queen) + " "); 16 | System.out.print((2 - rook) + " "); 17 | System.out.print((2 - bishop) + " "); 18 | System.out.print((2 - knight) + " "); 19 | System.out.print((8 - pawn) + " "); 20 | } 21 | 22 | public static class FastReader { 23 | BufferedReader br; 24 | StringTokenizer st; 25 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 26 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 27 | String next() { 28 | while (st == null || !st.hasMoreElements()) { 29 | try { st = new StringTokenizer(br.readLine()); } 30 | catch (IOException e) { e.printStackTrace(); } 31 | } 32 | return st.nextToken(); 33 | } 34 | int nextInt() { return Integer.parseInt(next()); } 35 | long nextLong() { return Long.parseLong(next()); } 36 | double nextDouble() { return Double.parseDouble(next()); } 37 | String nextLine() { 38 | String str = ""; 39 | try { str = br.readLine(); } 40 | catch (IOException e) { e.printStackTrace(); } 41 | return str; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /4344.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) throws IOException { 6 | FastReader fr = new FastReader(); 7 | int c = fr.nextInt(); 8 | for (int testCase = 0; testCase < c; testCase++) { 9 | int n = fr.nextInt(); // 학생의 수 10 | ArrayList arrayList = new ArrayList(); 11 | for (int i = 0; i < n; i++) { 12 | arrayList.add(fr.nextInt()); // 각 점수 입력 13 | } 14 | // 한 명씩 점수를 확인하여 합계 계산 15 | int sum = 0; 16 | for (int i = 0; i < n; i++) { 17 | sum += arrayList.get(i); 18 | } 19 | // 평균을 넘는 학생의 수 계산 20 | double average = (double) sum / n; 21 | int count = 0; 22 | for (int i = 0; i < n; i++) { 23 | if (arrayList.get(i) > average) count++; 24 | } 25 | // 평균을 넘는 학생의 비율 계산 26 | double ratio = (double) count * 100 / n; 27 | // 소수점 아래 셋째자리까지 출력 28 | String answer = String.format("%.3f", ratio); 29 | System.out.println(answer + "%"); 30 | } 31 | } 32 | public static class FastReader { 33 | BufferedReader br; 34 | StringTokenizer st; 35 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 36 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 37 | String next() { 38 | while (st == null || !st.hasMoreElements()) { 39 | try { st = new StringTokenizer(br.readLine()); } 40 | catch (IOException e) { e.printStackTrace(); } 41 | } 42 | return st.nextToken(); 43 | } 44 | int nextInt() { return Integer.parseInt(next()); } 45 | long nextLong() { return Long.parseLong(next()); } 46 | double nextDouble() { return Double.parseDouble(next()); } 47 | String nextLine() { 48 | String str = ""; 49 | try { str = br.readLine(); } 50 | catch (IOException e) { e.printStackTrace(); } 51 | return str; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /6987.py: -------------------------------------------------------------------------------- 1 | """ 2 | [문제 해결 아이디어] 3 | * 전체 나라의 개수가 6개 밖에 없으므로, 4 | 가능한 두 나라의 쌍 (X, Y)에 대하여 5 | 승/무/패 여부를 고려한다. 6 | 7 | 즉, 하나의 쌍 (X, Y)가 주어졌을 때, 가능한 경우의 수는 3개다. (승/무/패) 8 | 이때, 총 6C2 = 15개의 쌍이 있으므로, 모든 조합에 대하여 고려하면 9 | 3^15가지 경우의 수가 있는 것이다. 10 | 11 | 3^3을 27 => 30이라고 보자. 12 | 결과적으로 30 * 30 * 30 * 30 * 30의 경우의 수 13 | => 대략적으로약 2천만 번 정도의 연산이 필요 14 | 15 | * 구현을 어떻게 해야 하는가? 16 | 0 1 2 3 4 17 | (A, B), (A, C), (A, D), (A, E), (A, F) 18 | 5 6 7 8 19 | (B, C), (B, D), (B, E), (B, F) 20 | 9 10 11 21 | (C, D), (C, E), (C, F) 22 | 12 13 23 | (D, E), (D, F) 24 | 14 25 | (E, F) 26 | 15개이므로, 차례대로 27 | (왼쪽이 승이면 1, 무: 0, 왼쪽이 패면 -1) 28 | arr[15] = [1, -1, 0, ...] 이렇게 표현하면? 29 | 30 | A 기준: arr[0], arr[1], arr[2], arr[3], arr[4] 보기 31 | B 기준: -arr[0], arr[5], arr[6], arr[7], arr[8] 보기 32 | C 기준: -arr[1], -arr[5], arr[9], arr[10], arr[11] 보기 33 | D 기준: -arr[2], -arr[6], -arr[9], arr[12], arr[13] 보기 34 | E 기준: -arr[3], -arr[7], -arr[10], -arr[12], arr[14] 35 | 36 | index_a = [0, 1, 2, 3, 4] 37 | index_b = [-0, 5, 6, 7, 8] 38 | index_c = [-1, -5, 9, 10, 11] 39 | index_d = [-2, -6, -9, 12, 13] 40 | index_e = [-3, -7, -10, -12, 14] 41 | """ 42 | 43 | import sys 44 | 45 | sys.setrecursionlimit(int(1e8)) 46 | 47 | index_a = [0, 1, 2, 3, 4] 48 | index_b = [-0, 5, 6, 7, 8] 49 | index_c = [-1, -5, 9, 10, 11] 50 | index_d = [-2, -6, -9, 12, 13] 51 | index_e = [-3, -7, -10, -12, 14] 52 | index_f = [-4, -8, -11, -13, -14] 53 | 54 | 55 | def possible(country, indices): 56 | win, draw, lose = country 57 | # 현재 조합에 대하여 확인 58 | for i in indices: 59 | if i >= 0: 60 | # 승리했다면 61 | if result[i] == 1: 62 | win -= 1 63 | # 무승부라면 64 | elif result[i] == 0: 65 | draw -= 1 66 | # 패배했다면 67 | else: 68 | lose -= 1 69 | if i < 0: 70 | # 승리했다면 71 | if result[i] == -1: 72 | win -= 1 73 | # 무승부라면 74 | elif result[i] == 0: 75 | draw -= 1 76 | # 패배했다면 77 | else: 78 | lose -= 1 79 | return (win == 0) and (draw == 0) and (lose == 0) 80 | 81 | 82 | def dfs(depth, start): 83 | global check 84 | # 현재 조합에 대하여 결과 계산 85 | if depth == 15: 86 | # print(result) 87 | if not possible(a, index_a): return 88 | if not possible(b, index_b): return 89 | if not possible(c, index_c): return 90 | if not possible(d, index_d): return 91 | if not possible(e, index_e): return 92 | if not possible(f, index_f): return 93 | # 가능한 경우이다. 94 | check = True 95 | return 96 | for i in range(start, 15): 97 | if visited[i]: continue 98 | # (승, 무, 패)에 대한 모든 경우의 수 계산 99 | for cur in [1, 0, -1]: 100 | visited[i] = True 101 | result.append(cur) 102 | dfs(depth + 1, i + 1) 103 | visited[i] = False 104 | result.pop() 105 | 106 | 107 | for _ in range(4): 108 | data = list(map(int, input().split())) 109 | a = data[0:3] 110 | b = data[3:6] 111 | c = data[6:9] 112 | d = data[9:12] 113 | e = data[12:15] 114 | f = data[15:18] 115 | 116 | check = False 117 | visited = [False] * 15 118 | result = [] 119 | dfs(0, 0) 120 | if check: print(1, end=" ") 121 | else: print(0, end=" ") 122 | -------------------------------------------------------------------------------- /9184.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | 6 | public static int[][][] d = new int[21][21][21]; 7 | 8 | public static int w(int a, int b, int c) { 9 | // 종료 조건 10 | if (a <= 0 || b <= 0 || c <= 0) return 1; 11 | if (a > 20 || b > 20 || c > 20) return w(20, 20, 20); 12 | 13 | // 이미 구한 적이 있는 함수라면 14 | if (d[a][b][c] != 0) return d[a][b][c]; 15 | 16 | // 점화식에 따라서 계산 17 | if (a < b && b < c) { 18 | d[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c); 19 | } 20 | else { 21 | d[a][b][c] = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1); 22 | } 23 | return d[a][b][c]; 24 | } 25 | 26 | public static void main(String[] args) { 27 | FastReader fr = new FastReader(); 28 | 29 | while (true) { 30 | int a = fr.nextInt(); 31 | int b = fr.nextInt(); 32 | int c = fr.nextInt(); 33 | if (a == -1 && b == -1 && c == -1) break; 34 | else System.out.println("w(" + a + ", " + b + ", " + c + ") = " + w(a, b, c)); 35 | } 36 | } 37 | public static class FastReader { 38 | BufferedReader br; 39 | StringTokenizer st; 40 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 41 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 42 | String next() { 43 | while (st == null || !st.hasMoreElements()) { 44 | try { st = new StringTokenizer(br.readLine()); } 45 | catch (IOException e) { e.printStackTrace(); } 46 | } 47 | return st.nextToken(); 48 | } 49 | int nextInt() { return Integer.parseInt(next()); } 50 | long nextLong() { return Long.parseLong(next()); } 51 | double nextDouble() { return Double.parseDouble(next()); } 52 | String nextLine() { 53 | String str = ""; 54 | try { str = br.readLine(); } 55 | catch (IOException e) { e.printStackTrace(); } 56 | return str; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | FastReader fr = new FastReader(); // 문제 해결 소스 코드 7 | int a = fr.nextInt(); 8 | int b = fr.nextInt(); 9 | System.out.println(a + b); 10 | } 11 | public static class FastReader { 12 | BufferedReader br; 13 | StringTokenizer st; 14 | public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } 15 | public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } 16 | String next() { 17 | while (st == null || !st.hasMoreElements()) { 18 | try { st = new StringTokenizer(br.readLine()); } 19 | catch (IOException e) { e.printStackTrace(); } 20 | } 21 | return st.nextToken(); 22 | } 23 | int nextInt() { return Integer.parseInt(next()); } 24 | long nextLong() { return Long.parseLong(next()); } 25 | double nextDouble() { return Double.parseDouble(next()); } 26 | String nextLine() { 27 | String str = ""; 28 | try { str = br.readLine(); } 29 | catch (IOException e) { e.printStackTrace(); } 30 | return str; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### BOJ Java 2 | 3 | > BOJ Solutions in Java 4 | 5 | #### Usage 6 | 7 | * The basic template: [(code)](./Main.java) 8 | --------------------------------------------------------------------------------