Algorithm
-
백준 15953(JAVA) - 상금헌터Algorithm 2022. 11. 24. 17:04
15953번: 상금 헌터 (acmicpc.net) 15953번: 상금 헌터 첫 번째 줄에 제이지가 상상력을 발휘하여 가정한 횟수 T(1 ≤ T ≤ 1,000)가 주어진다. 다음 T개 줄에는 한 줄에 하나씩 제이지가 해본 가정에 대한 정보가 주어진다. 각 줄에는 두 개의 음이 아닌 www.acmicpc.net 15953 상금 헌터 문제이다. 문제 자체는 엄청나게 길어서 살짝 쫄고 들어갈 수 있는 부분이 있다. 하지만, 사실 읽어보면 그냥 if else if else 만 한 참 동안 해주면 되는 문제이다 보니 그리 힘들이지 않고 금방 풀 수 있었다. 범위를 이상하게 잡는 실수 때문에 시간이 조금 끌렸지만 기본적인 알고리즘은 거의 5분 안에 생각했던 거 같다. package selftest; import ja..
-
백준 2751(JAVA) - 수 정렬하기2Algorithm 2022. 11. 22. 16:08
2751번: 수 정렬하기 2 (acmicpc.net) 2751번: 수 정렬하기 2 첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 절댓값이 1,000,000보다 작거나 같은 정수이다. 수는 중복되지 않는다. www.acmicpc.net 백준 2751 수 정렬하기 2 문제이다. 이 문제는 순수하게 그냥 나열해두고 sorting만 해주면 끝나는 문제이다. 처음에는 Arrays.sort()로 실시했는데 시간초과가 나서 Collections.sort()로 실시했다. 사실 그래도 시간초과가 났다. 이해가 잘 안되어서 고민을 했는데 StringBuilder로 해보니까 바로 정답을 맞출 수 있었다. package selftest; import..
-
백준 2810(JAVA) - 컵홀더Algorithm 2022. 11. 17. 09:38
2810번: 컵홀더 (acmicpc.net) 2810번: 컵홀더 첫째 줄에 좌석의 수 N이 주어진다. (1 ≤ N ≤ 50) 둘째 줄에는 좌석의 정보가 주어진다. www.acmicpc.net 오늘 풀어볼 문제는 백준 2810 컵홀더 문제이다. 문제에서 원하는 정보는 굉장히 간단하다. S면 카운팅해주고, L이면 한 칸 넘기고 카운팅해달라는 뜻이다. 단, SSS인 경우와 같이 모든 좌석보다 카운팅 수가 커지는 경우에는 좌석 갯수를 그대로 출력시키면 된다. package selftest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main_2810 { public ..
-
백준 2512(JAVA) - 예산Algorithm 2022. 11. 15. 20:16
2512번: 예산 (acmicpc.net) 2512번: 예산 첫째 줄에는 지방의 수를 의미하는 정수 N이 주어진다. N은 3 이상 10,000 이하이다. 다음 줄에는 각 지방의 예산요청을 표현하는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 값들은 모두 1 이상 www.acmicpc.net 오늘의 문제는 실버3 예산문제이다. 분명 실버5~브론즈 문제정도를 출제하자고 합의를 보았는데... 대체 누군지 모르겠지만 갑자기 실버3 문제를 내었다. 예산 문제를 푸는데 처음에 어떻게 하면 좋을까 생각하다가 탐색 기법을 사용하기로 했다. 탐색기법을 사용할 때 완전탐색보다는 이진탐색을 사용하자는 생각으로 자료구조에서 배웠던 것을 떠올려 문제를 풀어보았다. 알고리즘 1. N을 입력받는다. 2. StringTokeni..
-
백준 10814(JAVA) - 나이순 정렬Algorithm 2022. 11. 14. 13:34
10814번: 나이순 정렬 (acmicpc.net) 10814번: 나이순 정렬 온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 www.acmicpc.net 나이순 정렬 문제이다. 이게 왜 실버 5인지는 잘 모르겠는 문제이다. 꽤나 어렵다고 느꼈고 StringBuilder를 사용해서 거의 억지로 끼워맞춰 푼 문제였다. 문제는 간단하다. 앞의 나이와 이름을 입력받고, 나이를 비교하여 어린 순서대로 정렬시킨다. 또한, 나이가 같으면 먼저 입력받아진 순서대로 위에 먼저 위치시킨다. 알고리즘 1. N을 입력받는다.(BufferedReader 사용) 2. StringBuilder ..
-
백준 4673(JAVA) - 셀프 넘버Algorithm 2022. 11. 14. 13:16
4673번: 셀프 넘버 (acmicpc.net) 4673번: 셀프 넘버 셀프 넘버는 1949년 인도 수학자 D.R. Kaprekar가 이름 붙였다. 양의 정수 n에 대해서 d(n)을 n과 n의 각 자리수를 더하는 함수라고 정의하자. 예를 들어, d(75) = 75+7+5 = 87이다. 양의 정수 n이 주어졌을 때, www.acmicpc.net 오늘의 문제는 셀프 넘버 이다. 뭔가 생성자라는 느낌이 이전에 풀어봤던 문제와 유사해서 쉽게 접근할 수 있었다. 생성자를 만드는 d 함수를 하나 만들어 주고 d 함수를 boolean 배열 속에 담으면 간단하게 끝나는 문제였다. 알고리즘 1. 생성자 함수를 while(num != 0) 까지 반복문을 돌리면서 더하고 sum을 반환시키는 것으로 만들어준다. 2. bool..
-
백준 1789(JAVA) - 수들의 합Algorithm 2022. 11. 10. 09:49
1789번: 수들의 합 (acmicpc.net) 1789번: 수들의 합 첫째 줄에 자연수 S(1 ≤ S ≤ 4,294,967,295)가 주어진다. www.acmicpc.net 오랜만에 중간고사 시험을 종료하고 다시 코테의 세계로 돌아왔다. 오늘 풀어본 문제는 백준 1789번 수들의 합 문제이다. 이 문제는 문제 길이 만큼이나 생각을 쉽게 하면 매우 간단하게 풀린다. 서로 다른 N개의 자연수의 합이 S라고 한다. S를 알 때, 자연수 N의 최댓값은 얼마일까? 이게 문제인데 예를 들어, S=200이라고 해 보자. S가 200이 되기 위해서는 1+2+3+ ... +19가 되어야 한다. 이 때 출력값 N=19가 되는 것이다. 알고리즘 1. long형으로 S를 입력받는다. 2. 합을 보여주기 위한 sum 변수와 ..
-
백준 14659 - 한조서열정리하고옴ㅋㅋ(JAVA)Algorithm 2022. 10. 5. 20:13
14659번: 한조서열정리하고옴ㅋㅋ (acmicpc.net) 14659번: 한조서열정리하고옴ㅋㅋ 첫째 줄에 봉우리의 수 겸 활잡이의 수 N이 주어진다. (1 ≤ N ≤ 30,000) 둘째 줄에 N개 봉우리의 높이가 왼쪽 봉우리부터 순서대로 주어진다. (1 ≤ 높이 ≤ 100,000) 각각 봉우리의 높이는 중복 없이 www.acmicpc.net 이 문제는 서론이 상당히 길다. 쓸데 없는 서론을 제외하면 문제가 굉장히 단순하다. 두 가지 조건을 잊지 않으면 된다. 1. 처음보다 더 높은 곳은 포기, 낮은 곳은 킬 2. 그 중 최대를 구하라 이렇게 두 가지를 잊지 않고 알고리즘을 구현해보자. ※ 알고리즘 1. 배열로 필요한 사항들을 반복적으로 입력받는다. 2. 이중 for문을 이용하여 브루트포스 형식으로 완전..