Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions problems/SWEA/p1233/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* (1233) [S/W 문제해결 기본] 9일차 - 사칙연산 유효성 검사
* https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV141176AIwCFAYD&categoryId=AV141176AIwCFAYD&categoryType=CODE&problemTitle=1233&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1
*/

package swea.p1233;

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 - 1233. [S/W 문제해결 기본] 9일차 - 사칙연산 유효성 검사
* @author YeJun, Jung
*
* @see #main(String[])
* 1. 솔루션을 실행한다.
*
* @see #input()
* 2. 노드의 개수를 입력받아 nodeCount에 저장한다.
* 3. 그래프를 저장할 graphValues, graphChildren 배열을 nodeCount 개수 만큼 할당한다.
* 4. nodeCount 만큼 반복하면서 노드 데이터를 입력받는다.
* 5. 노드 값은 graphValues 배열에 저장한다.
* 6. 노드의 자식은 graphChildren 배열에 저장한다.
*
* @see #solve()
* 7. 문제에서 말하는 올바른 그래프가 되기 위해서 숫자는 모두 리프노드에 위치해야 하며, 리프노드 외의 나머지 노드들은
* 연산자를 값으로 가지고 있어야 한다.
* 8. 정답 변수 answer의 값을 0으로 초기화한다.
* 9. 모든 노드를 방문한다. (노드의 번호는 1부터 시작한다)
* 9-1. 현재 노드가 리프노드인지 여부를 isLeafNode에 저장하고, 현재 노드의 값이 숫자인지 여부를 isNumber에 저장한다.
* 9-2. 현재 노드가 (리프노드인데 숫자가 아니거나), (리프노드가 아닌데 숫자라면) 잘못된 구조이므로 answer가 0인 상태로 함수를 종료한다.
* 10. 그래프 구조가 정상인 경우 answer값으로 1로 변경한다.
*
* @see #print()
* 11. answer값을 화면에 출력한다.
*/
public class Solution {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer line;

public static void main(String[] args) throws IOException {
final int testCount = 10;

for (int testCase = 1; testCase <= testCount; testCase++) {
// 1. 솔루션을 실행한다.
new Solution(testCase).run();
}
}

private static void getLine() throws IOException {
line = new StringTokenizer(reader.readLine().trim());
}

// --------------------------------------------------------

private int testCase;
private int answer;

private int nodeCount;
private String[] graphValues; // 노드 번호는 1부터 시작한다
private int[][] graphChildren; // 왼쪽, 오른쪽 순서로 저장(자식이 없다면 0 저장)

public Solution(int testCase) {
this.testCase = testCase;
}

public void run() throws IOException {
input();
solve();
print();
}

private void input() throws IOException {
// 2. 노드의 개수를 입력받아 nodeCount에 저장한다.
nodeCount = Integer.parseInt(reader.readLine().trim());

// 3. 그래프를 저장할 graphValues, graphChildren 배열을 nodeCount 개수 만큼 할당한다.
graphValues = new String[nodeCount + 1];
graphChildren = new int[nodeCount + 1][2];

// 4. nodeCount 만큼 반복하면서 노드 데이터를 입력받는다.
for (int lineCount = 0; lineCount < nodeCount; lineCount++) {
getLine();

// 5. 노드 값은 graphValues 배열에 저장한다.
int nodeIndex = Integer.parseInt(line.nextToken());
graphValues[nodeIndex] = line.nextToken();

// 6. 노드의 자식은 graphChildren 배열에 저장한다.
if (line.hasMoreTokens()) graphChildren[nodeIndex][0] = Integer.parseInt(line.nextToken());
if (line.hasMoreTokens()) graphChildren[nodeIndex][1] = Integer.parseInt(line.nextToken());
}
}

private void solve() {
// 7. 문제에서 말하는 올바른 그래프가 되기 위해서 숫자는 모두 리프노드에 위치해야 하며, 리프노드 외의 나머지 노드들은
// 연산자를 값으로 가지고 있어야 한다.

// 8. 정답 변수 answer의 값을 0으로 초기화한다.
answer = 0;

// 9. 모든 노드를 방문한다. (노드의 번호는 1부터 시작한다)
for (int index = 1; index <= nodeCount; index++) {
// 9-1. 현재 노드가 리프노드인지 여부를 isLeafNode에 저장하고, 현재 노드의 값이 숫자인지 여부를 isNumber에 저장한다.
boolean isLeafNode = graphChildren[index][0] == 0 && graphChildren[index][1] == 0;
boolean isNumber = Character.isDigit(graphValues[index].charAt(0));

// 9-2. 현재 노드가 (리프노드인데 숫자가 아니거나), (리프노드가 아닌데 숫자라면) 잘못된 구조이므로 answer가 0인 상태로 함수를 종료한다.
if (isLeafNode ^ isNumber) return;
}

// 10. 그래프 구조가 정상인 경우 answer값으로 1로 변경한다.
answer = 1;
}

private void print() throws IOException {
// 11. answer값을 화면에 출력한다.
writer.write("#" + testCase + " " + answer + "\n");
writer.flush();
}
}
137 changes: 137 additions & 0 deletions problems/SWEA/p9229/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* (9229) 한빈이와 Spot Mart
* https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AW8Wj7cqbY0DFAXN&categoryId=AW8Wj7cqbY0DFAXN&categoryType=CODE&problemTitle=9229&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1
*/

package swea.p9229;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
* SW Expert Academy - 9229. 한빈이와 Spot Mart
* @author YeJun, Jung
*
* @see #main(String[])
* 1. 테스트 케이스를 입력받는다.
* 2. 솔루션을 실행한다.
*
* @see #input()
* 3. 과자의 개수, 제한 무게를 입력받는다.
* 4. 과자 무게를 저장할 배열 snacks를 과자의 개수만큼 초기화 한다.
* 5. 과자 무게를 입력받아 snacks에 저장한다.
*
* @see #solve()
* 6. snacks 배열을 오름차순 정렬 한다.
* 7. 정답 변수 answer를 -1로 초기화 한다. (정답이 없는 경우 -1을 출력하기 위함)
* 8. snacks 배열 index를 저장할 p0, p1을 초기화 한다.
* 9. 중간 계산 결과를 저장할 current 변수를 선언한다.
* 10. p0이 p1보다 왼쪽(작은) 상태가 유지될때까지 반복한다.
* 11. snacks 배열의 p0, p1 index에 위치한 값들을 더해서 current 변수에 저장한다.
* 12. current 값이 무게 제한보다 큰 경우 p1을 왼쪽으로 줄인다.
* 13. current 값이 무게 제한 안에 있는 경우
* 13-1. answer 값과 current 값을 비교하여 큰 값으로 answer 변수를 갱신한다.
* 13-2. p0을 오른쪽으로 키운다.
*
* @see #print()
* 14. answer 값을 화면에 출력한다.
*/
public class Solution {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer line;

public static void main(String[] args) throws IOException {
// 1. 테스트 케이스를 입력받는다.
int testCount = Integer.parseInt(reader.readLine().trim());

for (int testCase = 1; testCase <= testCount; testCase++) {
// 2. 솔루션을 실행한다.
new Solution(testCase).run();
}
}

private static StringTokenizer getLine() throws IOException {
line = new StringTokenizer(reader.readLine().trim());
return line;
}

// --------------------------------------------------------

private int testCase;
private int answer;

private int snackCount;
private int weightLimit;
private int[] snacks;

public Solution(int testCase) {
this.testCase = testCase;
}

public void run() throws IOException {
input();
solve();
print();
}

private void input() throws IOException {
// 3. 과자의 개수, 제한 무게를 입력받는다.
getLine();
snackCount = Integer.parseInt(line.nextToken());
weightLimit = Integer.parseInt(line.nextToken());

// 4. 과자 무게를 저장할 배열 snacks를 과자의 개수만큼 초기화 한다.
snacks = new int[snackCount];

// 5. 과자 무게를 입력받아 snacks에 저장한다.
getLine();
for (int index = 0; index < snackCount; index++) {
snacks[index] = Integer.parseInt(line.nextToken());
}
}

private void solve() {
// 6. snacks 배열을 오름차순 정렬 한다.
Arrays.sort(snacks);

// 7. 정답 변수 answer를 -1로 초기화 한다. (정답이 없는 경우 -1을 출력하기 위함)
answer = -1;

// 8. snacks 배열 index를 저장할 p0, p1을 초기화 한다.
int p0 = 0;
int p1 = snackCount - 1;

// 9. 중간 계산 결과를 저장할 current 변수를 선언한다.
int current;

// 10. p0이 p1보다 왼쪽(작은) 상태가 유지될때까지 반복한다.
while (p0 < p1) {
// 11. snacks 배열의 p0, p1 index에 위치한 값들을 더해서 current 변수에 저장한다.
current = snacks[p0] + snacks[p1];

if (current > weightLimit) {
// 12. current 값이 무게 제한보다 큰 경우 p1을 왼쪽으로 줄인다.
p1--;
} else {
// 13. current 값이 무게 제한 안에 있는 경우

// 13-1. answer 값과 current 값을 비교하여 큰 값으로 answer 변수를 갱신한다.
answer = Math.max(answer, current);
// 13-2. p0을 오른쪽으로 키운다.
p0++;
}
}
}

private void print() throws IOException {
// 14. answer 값을 화면에 출력한다.
writer.write("#" + testCase + " " + answer + "\n");
writer.flush();
}
}