[알고리즘]/백준 85

백준 9012 자바 - 괄호

문제 문제해결 import java.util.Scanner; import java.util.Stack; public class B9012 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++) { String str = sc.next(); Stack stack = new Stack(); //괄호를 담을 스텍 boolean flag = true; for (int j = 0; j < str.length(); j++) { if (str.charAt(j)=='(') stack.push('('); //여는 괄호 넣기 else if (sta..

백준 10828 자바 - 스택

문제 문제해결 import java.util.Scanner; public class B10828 { public static int[] stack; public static int size = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder sb = new StringBuilder();//출력에 사용될것 int n = sc.nextInt(); stack = new int[n]; for (int i = 0; i < n; i++) { String str = sc.next(); switch (str){ case "push": push(sc.nextInt());//push일 경우 한번 더 입..

자바 - 구현 - 백준 9093 단어 뒤집기

문제 문제해결 import java.util.Scanner; public class B9093 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = Integer.parseInt(sc.nextLine()); String[] results = new String[t]; // 결과를 담을 배열 for (int i = 0; i < t; i++) { String word = sc.nextLine(); String[] wordArr = word.split(" "); StringBuilder result = new StringBuilder(); for (String words : wordArr) { for (in..

자바 - 구현 - 백준 1783 병든 나이트

문제 문제해결 import java.util.Scanner; /* 1번: 2칸 위로, 1칸 오른쪽 2번: 1칸 위로, 2칸 오른쪽 3번: 1칸 아래로, 2칸 오른쪽 4번: 2칸 아래로, 1칸 오른쪽 */ public class S4_1783 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int cnt = 0; if (n==1){//세로칸이 1일경우 이동 불가 cnt = 1; } else if (n==2){//세로칸이 2일경우 2,3번 방향으로만 이동 가능, 1~4번 전부 이동 불가능 cnt = Math.min((m+1)/2, 4);..

자바 - 구현 - 백준 6986 절사평균

문제 문제해결 import java.util.Arrays; import java.util.Scanner; public class S4_6986 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextDouble(); } //입력 받은 점수 정렬 Arrays.sort(arr); //절사평균 구하기 double sum = 0.0; for (int i = k; i < n-k; i++) {//앞뒤로 k개만큼 빼고 ..

자바 - 구현 - 백준 1292 쉽게 푸는 문제

문제 문제해결 import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class S4_1292 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); // 수열을 담을 배열 List list = new ArrayList(); // max가 1000이므로 넉넉히 50까지 수열을 구한다 for (int i = 1; i < 50; i++) { for (int j = 0; j < i; j++) { list.add(i); } } // a~b 사이의 합 구하..

자바 - 구현 - 백준 1978 소수 찾기

문제 문제해결 import java.util.Scanner; public class S4_1978 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //배열 입력 받기 int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = sc.nextInt(); } //소수 개수 담을 변수 int cnt = 0; for (int i = 0; i < n; i++) {//배열을 하나씩 돌면서 소수인지 판별한다 int j = 2; for (; j < nums[i]; j++) { if (nums[i] % j == 0) break; } ..

자바 - 구현 - 백준 2693 N번째 큰 수

문제 문제해결 import java.util.Arrays; import java.util.Scanner; public class S5_2693 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); //10개의 숫자를 담을 배열 int[] arr = new int[10]; //3번째 큰수를 담을 배열 int[] results = new int[n]; //각 배열을 정렬 후 7번째 작은 수 출력(3번째 큰수) for (int i = 0; i < n; i++) { for (int j = 0; j < 10; j++) { arr[j] = sc.nextInt(); } Arrays.sort..