알고리즘
-
숫자야구알고리즘 2023. 6. 15. 19:54
import java.util.*; public class TeamWork { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 서로 다른 3개의 숫자를 기억하는 것만들기 Set set = new HashSet(); // 중복을 허용 안함. 순서를 중요하게 생각 x while (true) { if(set.size() == 3){ break; } set.add((int) (Math.random() * 10)); } List list = new ArrayList(set); System.out.println("list = " + list); int ball; int strike = 0; int count = 0; S..
-
2798 - 블랙잭알고리즘 2023. 5. 30. 19:58
https://www.acmicpc.net/problem/2798 2798번: 블랙잭 첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장 www.acmicpc.net 배열을 모두 돌면서 가장 큰 합이 무엇인지 확인하는 문제 3개의 숫자를 골라야 하므로 3개의 for문을 돌면서, 중복이 되지 않아야하므로, 시작과 종료조건을 조금씩 건드렸다. 모든 조건을 돌면서 가장 큰 것을 담을 수 있는 변수를 만들어 남아놨다. import java.io.BufferedReader; import java.io.IOException; import ..
-
11866 - 요세푸스 문제 0알고리즘 2023. 5. 30. 19:49
https://www.acmicpc.net/problem/11866 11866번: 요세푸스 문제 0 첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000) www.acmicpc.net 문제요약 1 부터 주어진 숫자까지의 숫자가 나열 다음 주어진 숫자만큼 건너 뛰면서 해당 숫자를 제거하고 출력 이전에 회전하는 큐와 비슷한 문제 접근으로 할 수 있다는 것을 알았다. 왜냐면 3번째 숫자 전까지 옮겨주고, 3번째 숫자를 제거 한뒤 이를 기록하는 방식으로 접근했다면 쉬웠을 것이다. 하지만, 해당 방법은 추후에 생각해 낸 것이고 현재는 규칙을 찾아 시도했다. 1 2 3 4 5 6 7 이라고 가정하면, 1. 오른쪽으로 계속 진행하는 경우, 이경우는 제거 인덱스를 지속적으로 ..
-
11279 - 최대 힙알고리즘 2023. 5. 30. 19:35
https://www.acmicpc.net/problem/11279 11279번: 최대 힙 첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0 www.acmicpc.net 문제요약 가장 큰수를 계속해서 지우거나, 비어있을 경우 0을 출력하는 문제 내가 가지고 있는 데이터가 계속해서 정렬시켜야하는 문제이다. 그리고 제거도 해야하므로, PriorityQueue를 역순으로 적용하는 작업을 했다. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamR..
-
4949 - 균형잡힌 세상알고리즘 2023. 5. 30. 19:29
https://www.acmicpc.net/problem/4949 4949번: 균형잡힌 세상 각 문자열은 마지막 글자를 제외하고 영문 알파벳, 공백, 소괄호("( )"), 대괄호("[ ]")로 이루어져 있으며, 온점(".")으로 끝나고, 길이는 100글자보다 작거나 같다. 입력의 종료조건으로 맨 마지막에 www.acmicpc.net 문제요약 [ ] ,( ) 대로 괄호 쌍이 맞아야 하는 문제 이전에 풀었던 문제와 비슷 stack에 쌓으면서 괄호쌍이 맞으면 제거하는 식으로 진행하였다. 만일 괄호쌍이 전부 제거된 상태에서 닫는 괄호( ) , ] )가 나온다면 바로 멈추는 작업을 추가했다. import java.io.BufferedReader; import java.io.IOException; import ja..
-
9012 - 괄호알고리즘 2023. 5. 26. 21:18
https://www.acmicpc.net/problem/9012 9012번: 괄호 괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고 www.acmicpc.net 문제요약 문장 내의 () [] 괄호 쌍이 올바르게 되어 있는지 확인하는 문제 마침표( . )이 첫 문자열로 들어오면 종료 해당문제는 괄호 쌍이 맞는 지를 확인하는 문제였다. 괄호쌍이 맞는다는 것은 - 여는 괄호 -> 닫는 괄호 쌍을 찾는 것 이 핵심이므로, stack에 두 개의 쌍이 나오면 pop()을 하여 제거하였고, 결과적으로 stack이 비어있는지를 체크하는 방..
-
1021 - 회전하는 큐알고리즘 2023. 5. 26. 19:55
https://www.acmicpc.net/problem/1021 1021번: 회전하는 큐 첫째 줄에 큐의 크기 N과 뽑아내려고 하는 수의 개수 M이 주어진다. N은 50보다 작거나 같은 자연수이고, M은 N보다 작거나 같은 자연수이다. 둘째 줄에는 지민이가 뽑아내려고 하는 수의 위치가 www.acmicpc.net 문제요약 1. 첫 번째 원소를 뽑아낸다. 이 연산을 수행하면, 원래 큐의 원소가 a1, ..., ak이었던 것이 a2, ..., ak와 같이 된다. 2. 왼쪽으로 한 칸 이동. 이 연산을 수행하면, a1, ..., ak가 a2, ..., ak, a1이 된다. 3. 오른쪽으로 한 칸 이동. 이 연산을 수행하면, a1, ..., ak가 ak, a1, ..., ak-1이 된다. 2번 3번의 연산을 ..
-
1874 - 스택 수열알고리즘 2023. 5. 26. 19:39
https://www.acmicpc.net/problem/1874 1874번: 스택 수열 1부터 n까지에 수에 대해 차례로 [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] 연산을 수행하면 수열 [4, 3, 6, 8, 7, 5, 2, 1]을 얻을 수 있다. www.acmicpc.net 문제요약 첫 줄에 n (1 ≤ n ≤ 100,000) 둘째 줄부터 n개의 줄에는 수열을 이루는 1이상 n이하의 정수가 하나씩 순서대로 주어짐 8 7 6 5 4 3 2 1 --> 오름차순으로 빼주면서, stack에 쌓고( push ), "+" 둘째줄에 입력된 값이 stack 에 가장 위에 쌓이면, 출력( pop ), ..