1) SW Expert Academy 정책상 문제 자체를 퍼가는 것은 금지되며 링크와 출처로 명시해 주시기 바랍니다.
2) 문제에 대한 본인의 풀이에 대해서는 개인 학습 등 상업적 용도가 아닌 경우에만 문제 출처와 함께 게시가 가능합니다.
※ 저작권 이슈가 있을 시 법적 제재를 받을 수 있으니 참고하여주시기 바랍니다.
문제 설명
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
나의 풀이
import java.util.Scanner;
import java.util.ArrayList;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
for(int test_case = 1; test_case <= 10; test_case++) // 총 10개의 테스트
{
// 배열할 경우 직접 배열 생성 -> 배열 복사 -> 참조변수 변경을 구현하는데는 비용이 들어가니 ArrayList 사용, 사실 LinkedList 사용해야함
int ciphertextLength = sc.nextInt(); // 원본 암호문의 길이 N
ArrayList<Integer> ciphertext = new ArrayList<>();// 원본 암호문
for(int i = 0; i < ciphertextLength; i++) {
ciphertext.add(i, sc.nextInt());
}
int commandLength = sc.nextInt(); // 명령어의 개수
for(int j = 0; j < commandLength; j++) {
String commandDetail = sc.next(); // I냐 D냐?
int commandIndex = sc.nextInt(); // 19
int numbersLength = sc.nextInt(); // 4
if(commandDetail.equals("I")) {
int[] numbers = new int[numbersLength];
for(int k = 0; k < numbersLength; k++) {
numbers[k] = sc.nextInt();
}
for(int m = 0; m < numbersLength; m++) {
ciphertext.add(commandIndex+m, numbers[m]);
}
}else if(commandDetail.equals("D")){
for(int n = 0; n < numbersLength; n++) {
ciphertext.remove(commandIndex);
}
}
}
System.out.print("#" + test_case);
for(int i = 0; i < 10; i++) {
System.out.print(" " + ciphertext.get(i) + " ");
}
}
}
}
기록
1.ArrayList에서 특정 위치 삭제는 ArrayList.remove(index) 사용
- public E remove(int index) // 해당 index의 값이 삭제
- public boolean remove(Object o) // ArrayList에서 해당 객체를 찾아서 첫번째로 나오는 값만 삭제
2.전체적인 설계를 먼저 잘 짜야함 명령문에 따라 분기 처리를 잘 해주기
개선된 코드
import java.util.*;
class Solution {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
for (int test_case = 1; test_case <= 10; test_case++) {
int ciphertextLength = sc.nextInt();
ArrayList<Integer> ciphertext = new ArrayList<>();
for (int i = 0; i < ciphertextLength; i++) {
ciphertext.add(sc.nextInt());
}
int commandLength = sc.nextInt();
for (int j = 0; j < commandLength; j++) {
String command = sc.next();
if (command.equals("I")) {
int x = sc.nextInt(); // 위치
int y = sc.nextInt(); // 개수
for (int k = 0; k < y; k++) {
ciphertext.add(x + k, sc.nextInt());
}
} else if (command.equals("D")) {
int x = sc.nextInt(); // 위치
int y = sc.nextInt(); // 개수
for (int k = 0; k < y; k++) {
ciphertext.remove(x);
}
}
}
System.out.print("#" + test_case);
for (int i = 0; i < 10; i++) {
System.out.print(" " + ciphertext.get(i));
}
System.out.println();
}
}
}
LinkedList 버전
import java.util.*;
class Solution {
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
for (int test_case = 1; test_case <= 10; test_case++) {
int ciphertextLength = sc.nextInt(); // 원본 암호문의 길이
LinkedList<Integer> ciphertext = new LinkedList<>();
for (int i = 0; i < ciphertextLength; i++) {
ciphertext.add(sc.nextInt());
}
int commandLength = sc.nextInt(); // 명령어의 개수
for (int j = 0; j < commandLength; j++) {
String command = sc.next();
if (command.equals("I")) {
int x = sc.nextInt(); // 삽입 위치
int y = sc.nextInt(); // 삽입할 개수
// 삽입 위치까지 iterator로 이동
ListIterator<Integer> it = ciphertext.listIterator(x);
for (int k = 0; k < y; k++) {
it.add(sc.nextInt());
}
} else if (command.equals("D")) {
int x = sc.nextInt(); // 삭제 위치
int y = sc.nextInt(); // 삭제할 개수
ListIterator<Integer> it = ciphertext.listIterator(x);
for (int k = 0; k < y && it.hasNext(); k++) {
it.next();
it.remove();
}
}
}
// 결과 출력 (처음 10개만)
System.out.print("#" + test_case);
Iterator<Integer> it = ciphertext.iterator();
for (int i = 0; i < 10 && it.hasNext(); i++) {
System.out.print(" " + it.next());
}
System.out.println();
}
}
}
'👨💻 Coding Test > SWEA' 카테고리의 다른 글
[SW Expert Academy/Java/D3] 1230.암호문3 (0) | 2025.04.24 |
---|---|
[SW Expert Academy/Java/D3] 1228.암호문1 (0) | 2025.04.22 |
[SW Expert Academy/Java/D3] 1216.회문2 (0) | 2025.04.21 |
[SW Expert Academy/Java/D3] 1221.GNS (0) | 2024.11.15 |
[SW Expert Academy/Java/D3] 1220.Magnetic (2) | 2024.11.12 |