From c521432b4245fe5595fb309ec76e7087ca7f6400 Mon Sep 17 00:00:00 2001 From: "YeJun, Jung" <31740224+yejun614@users.noreply.github.com> Date: Thu, 29 Jan 2026 17:59:15 +0900 Subject: [PATCH] feat: add SWEA solutions --- problems/SWEA/p1225/Solution.java | 144 ++++++++++++++++++++++++++++++ problems/SWEA/p3260/Solution.java | 95 ++++++++++++++++++++ problems/SWEA/p3499/Solution.java | 138 ++++++++++++++++++++++++++++ 3 files changed, 377 insertions(+) create mode 100644 problems/SWEA/p1225/Solution.java create mode 100644 problems/SWEA/p3260/Solution.java create mode 100644 problems/SWEA/p3499/Solution.java diff --git a/problems/SWEA/p1225/Solution.java b/problems/SWEA/p1225/Solution.java new file mode 100644 index 0000000..499dcb3 --- /dev/null +++ b/problems/SWEA/p1225/Solution.java @@ -0,0 +1,144 @@ +package p1225; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.StringTokenizer; + +/** + * SW Expert Academy - 1225. [S/W 문제해결 기본] 7일차 - 암호생성기 + * @author YeJun, Jung + * + * @see #main + * 1. 입출력을 초기화 한다. + * 2. 테스트 케이스를 입력받는다. + * 3. 솔루션을 실행한다. + * + * @see #Solution(int) + * 4. 멤버 변수를 초기화 한다. + * + * @see #run() + * 5. 입력, 해결, 출력 순서로 솔루션을 실행한다. + * + * @see #input() + * 6. 큐를 하나 준비한다. + * 7. 한 라인을 입력받는다. + * 8. 입력받은 내용을 white space 기준으로 분리한다. + * 9. 분리한 내용을 큐에 삽입한다. + * + * @see #solve() + * 10. 싸이클을 실행한다. + * 13. 큐에 저장된 데이터를 answer 배열로 옮긴다. + * + * @see #runCycle() + * 11. num 정수 변수를 0으로 초기화 한다. + * 12. 무한 반복하면서 + * 12-1. 5회 반복을 실시하면서 count 변수로 숫자를 센다. + * 12-2. 큐의 제일 앞에 있는 요소를 꺼내어 count만큼 뺀다. + * 12-3. 계산을 마친 요소가 0 이하라면 함수를 중단한다. + * 12-4. 계산을 마친 요소가 0 초과라면 큐에 삽입하고 다음 반복을 진행한다. + * + * @see #print() + * 14. 결과 배열 answer 내용을 화면에 출력한다. + */ +public class Solution { + static BufferedReader reader; + static BufferedWriter writer; + + public static void main(String[] args) throws IOException { + // 1. 입출력을 초기화 한다. + reader = new BufferedReader(new InputStreamReader(System.in)); + writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + int testCase = 0; + + while (testCase != 10) { + // 2. 테스트 케이스를 입력받는다. + testCase = Integer.parseInt(reader.readLine().trim()); + + // 3. 솔루션을 실행한다. + Solution solution = new Solution(testCase); + solution.run(); + } + } + + int testCase; + final int N = 8; // 주어지는 데이터는 8개로 고정됨 + final int CycleCount = 5; + Queue data; + int[] answer; + + public Solution(int testCase) { + // 4. 멤버 변수를 초기화 한다. + this.testCase = testCase; + } + + public void run() throws IOException { + // 5. 입력, 해결, 출력 순서로 솔루션을 실행한다. + input(); + solve(); + print(); + } + + private void input() throws IOException { + // 6. 큐를 하나 준비한다. + data = new ArrayDeque<>(); + + // 7. 한 라인을 입력받는다. + String input = reader.readLine().trim(); + + // 8. 입력받은 내용을 white space 기준으로 분리한다. + StringTokenizer inputTokenizer = new StringTokenizer(input); + + // 9. 분리한 내용을 큐에 삽입한다. + for (int index = 0; index < N; index++) { + data.offer(Integer.parseInt(inputTokenizer.nextToken())); + } + } + + private void solve() { + // 10. 싸이클을 실행한다. + runCycle(); + + // 13. 큐에 저장된 데이터를 answer 배열로 옮긴다. + answer = new int[N]; // 배열의 각 요소는 기본값 0으로 초기화 됨 + for (int index = 0; index < N - 1; index++) { + answer[index] = data.poll(); + } + } + + private void runCycle() { + // 11. num 정수 변수를 0으로 초기화 한다. + int num = 0; + + // 12. 무한 반복하면서 + while (true) { + // 12-1. 5회 반복을 실시하면서 count 변수로 숫자를 센다. + for (int count = 1; count <= CycleCount; count++) { + // 12-2. 큐의 제일 앞에 있는 요소를 꺼내어 count만큼 뺀다. + num = data.poll(); + num -= count; + + // 12-3. 계산을 마친 요소가 0 이하라면 함수를 중단한다. + if (num <= 0) return; + + // 12-4. 계산을 마친 요소가 0 초과라면 큐에 삽입하고 다음 반복을 진행한다. + data.offer(num); + } + } + } + + private void print() throws IOException { + // 14. 결과 배열 answer 내용을 화면에 출력한다. + writer.write("#" + testCase + " "); + for (int index = 0; index < answer.length; index++) { + writer.write(answer[index] + " "); + } + writer.write("\n"); + writer.flush(); + } +} diff --git a/problems/SWEA/p3260/Solution.java b/problems/SWEA/p3260/Solution.java new file mode 100644 index 0000000..2784664 --- /dev/null +++ b/problems/SWEA/p3260/Solution.java @@ -0,0 +1,95 @@ +package p3260; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.math.BigInteger; +import java.util.StringTokenizer; + +/** + * SW Expert Academy - 3260. 두 수의 덧셈 + * @author YeJun, Jung + * + * @see #main + * 1. 입출력 초기화를 한다. + * 2. 테스트 케이스를 입력 받는다. + * 3. 솔루션을 실행한다. + * + * @see #Solution(int) + * 4. 멤버 변수를 초기화 한다. + * + * @see #run() + * 5. 입력, 해결, 출력 순서로 솔루션을 실행한다. + * + * @see #input() + * 6. 한 라인을 입력받아 input 변수에 저장한다. + * 7. input 문자열 내용을 white space를 기준으로 분리한다. + * 8. BigInteger로 입력받은 정수를 A, B 변수에 저장한다. + * + * @see #solve() + * 9. A + B 결과를 answer 변수에 저장한다. + * + * @see #print() + * 10. answer 변수 내용을 화면에 출력한다. + */ +public class Solution { + static BufferedReader reader; + static BufferedWriter writer; + + public static void main(String[] args) throws IOException { + // 1. 입출력 초기화를 한다. + reader = new BufferedReader(new InputStreamReader(System.in)); + writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + // 2. 테스트 케이스를 입력 받는다. + int testCount = Integer.parseInt(reader.readLine().trim()); + + for (int testCase = 1; testCase <= testCount; testCase++) { + // 3. 솔루션을 실행한다. + Solution solution = new Solution(testCase); + solution.run(); + } + } + + int testCase; + BigInteger A; + BigInteger B; + BigInteger answer; + + public Solution(int testCase) { + // 4. 멤버 변수를 초기화 한다. + this.testCase = testCase; + } + + public void run() throws IOException { + // 5. 입력, 해결, 출력 순서로 솔루션을 실행한다. + input(); + solve(); + print(); + } + + private void input() throws IOException { + // 6. 한 라인을 입력받아 input 변수에 저장한다. + String input = reader.readLine().trim(); + + // 7. input 문자열 내용을 white space를 기준으로 분리한다. + StringTokenizer inputTokenizer = new StringTokenizer(input); + + // 8. BigInteger로 입력받은 정수를 A, B 변수에 저장한다. + A = new BigInteger(inputTokenizer.nextToken()); + B = new BigInteger(inputTokenizer.nextToken()); + } + + private void solve() { + // 9. A + B 결과를 answer 변수에 저장한다. + answer = A.add(B); + } + + private void print() throws IOException { + // 10. answer 변수 내용을 화면에 출력한다. + writer.write("#" + testCase + " " + answer + "\n"); + writer.flush(); + } +} diff --git a/problems/SWEA/p3499/Solution.java b/problems/SWEA/p3499/Solution.java new file mode 100644 index 0000000..8858acf --- /dev/null +++ b/problems/SWEA/p3499/Solution.java @@ -0,0 +1,138 @@ +package p3499; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.util.StringTokenizer; + +/** + * SW Expert Academy - 3499. 퍼펙트 셔플 D3 + * @author YeJun, Jung + * + * @see #main(String[]) + * 1. 테스트 케이스 개수를 입력받는다. + * + * @see #Solution(int testCase) + * 2. 솔루션을 초기화한다. + * 2-1. 테스트 케이스를 저장한다. + * + * @see #run() + * 3. 솔루션을 실행한다. (입력, 해결, 출력) + * + * @see #input() + * 4. 자연수 N(1 <= N <= 1,000)을 입력받는다. + * 5. 카드를 입력받는다. + * 5-1. 카드를 문자열 배열에 입력받은 순서대로 cards 배열에 저장한다. + * + * @see #solve() + * 6. 정답을 저장할 문자열 배열 answer를 초기화 한다. + * 7. 입력받은 카드의 개수를 2로 나누어 halfCardLength 변수에 저장한다. + * 8. 정수를 저장하는 변수 indexA를 0으로 초기화한다. + * 9. 정수를 저장하는 변수 indexB를 halfCardLength으로 초기화한다. + * 10. cardLength가 짝수라면 indexB 변수에 +1을 더한다. + * 11. 1부터 halfCardLength까지 count 변수를 사용해서 반복한다. + * 11-1. cards[indexA], cards[indexB]를 answer 배열에 저장한다. + * 11-2. indexA, indexB 변수에 각각 +1을 더한다. + * 12. cardLength가 짝수라면 cards[indexA]를 answer 배열에 저장한다. + * + * @see #print() + * 13. 테스트 케이스를 화면에 출력한다. + * 14. answer 배열을 순회한다. + * 14-1. 배열 요소를 화면에 출력한다. + * 14-2. 마지막으로 \n을 화면에 출력한다. + */ +public class Solution { + static BufferedReader reader; + static BufferedWriter writer; + + public static void main(String[] args) throws IOException { + reader = new BufferedReader(new InputStreamReader(System.in)); + writer = new BufferedWriter(new OutputStreamWriter(System.out)); + + // 1. 테스트 케이스 개수를 입력받는다. + int testCount = Integer.parseInt(reader.readLine().trim()); + + for (int testCase = 1; testCase <= testCount; testCase++) { + // 2. 솔루션을 초기화한다. + Solution solution = new Solution(testCase); + solution.run(); + } + } + + int testCase; + int N; + String[] cards; + String[] answer; + + public Solution(int testCase) { + // 2-1. 테스트 케이스를 저장한다. + this.testCase = testCase; + } + + public void run() throws IOException { + // 3. 솔루션을 실행한다. (입력, 해결, 출력) + input(); + solve(); + print(); + } + + private void input() throws IOException { + // 4. 자연수 N(1 <= N <= 1,000)을 입력받는다. + N = Integer.parseInt(reader.readLine().trim()); + + // 5. 카드를 입력받는다. + cards = new String[N]; + + // 5-1. 카드를 문자열 배열에 입력받은 순서대로 cards 배열에 저장한다. + String cardsInput = reader.readLine().trim(); + StringTokenizer cardsTokenizer = new StringTokenizer(cardsInput); + + for (int index = 0; index < N; index++) { + cards[index] = cardsTokenizer.nextToken(); + } + } + + private void solve() throws IOException { + // 6. 정답을 저장할 문자열 배열 answer를 초기화 한다. + answer = new String[N]; + + // 7. 입력받은 카드의 개수를 2로 나누어 halfCardLength 변수에 저장한다. + int halfCardLength = N / 2; + + int index = 0; + int indexA = 0; // 8. 정수를 저장하는 변수 indexA를 0으로 초기화한다. + int indexB = halfCardLength; // 9. 정수를 저장하는 변수 indexB를 halfCardLength으로 초기화한다. + + // 10. cardLength가 짝수라면 indexB 변수에 +1을 더한다. + boolean isOdd = N % 2 != 0; + if (isOdd) indexB++; + + // 11. 1부터 halfCardLength까지 count 변수를 사용해서 반복한다. + for (int count = 0; count < halfCardLength; count++) { + // 11-1. cards[indexA], cards[indexB]를 answer 배열에 저장한다. + // 11-2. indexA, indexB 변수에 각각 +1을 더한다. + answer[index++] = cards[indexA++]; + answer[index++] = cards[indexB++]; + } + + // 12. cardLength가 짝수라면 cards[indexA]를 answer 배열에 저장한다. + if (isOdd) answer[index] = cards[indexA]; + } + + private void print() throws IOException { + // 13. 테스트 케이스를 화면에 출력한다. + writer.write("#" + testCase + " "); + + // 14. answer 배열을 순회한다. + for (int index = 0; index < N; index++) { + // 14-1. 배열 요소를 화면에 출력한다. + writer.write(answer[index] + " "); + } + + // 14-2. 마지막으로 \n을 화면에 출력한다. + writer.write("\n"); + writer.flush(); + } +}