1번 문제 - 빈칸 채우기

import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
System.out.println("Input correct number:");
System.out.printf("%d %d\n", 4256, 4948);
System.out.println("0000 0000" + "...passed");
}
}
2번 문제 - 디버깅

import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int numDiagonal = n * (n - 3) / 2;
System.out.println(numDiagonal);
}
}
// 기존 코드 int numDiagonal = n * n - 3 / 2;
3번 문제 - 빈칸 채우기

import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
int money = sc.nextInt();
int complete = sc.nextInt();
if(age >= 20 && age < 30){
money = (money / 100) * 10;
}
else if(age >= 30 && age < 40){
money = (money / 100) * 30;
}
else{
money = 0;
}
if(complete == 1){
money /= 2;
}
System.out.println(money);
}
}
4번 문제 - 디버깅

import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int start = sc.nextInt();
int step = sc.nextInt();
int end = sc.nextInt();
for(int i = start; i >= end; i -= step){
System.out.println(i);
}
}
}
// 기존 코드 for(int i = start; i < end; i++){
5번 문제 - 빈칸 채우기

class Solution {
public int[] solution(String menu, int n) {
int[] tomatoPasta = {4, 4, 1, 125};
int[] shrimpOilPasta = {6, 3, 0, 170};
int[] mushroomCreamPasta = {5, 4, 1, 140};
int[] answer = {0, 0, 0, 0};
if(menu.equals("tomato pasta")){
for(int i = 0; i < 4; i++){
answer[i] = tomatoPasta[i] * n;
}
}
else if(menu.equals("shrimp oil pasta")){
for(int i = 0; i < 4; i++){
answer[i] = shrimpOilPasta[i] * n;
}
}
else if(menu.equals("mushroom cream pasta")){
for(int i = 0; i < 4; i++){
answer[i] = mushroomCreamPasta[i] * n;
}
}
return answer;
}
}
6번 문제 - 디버깅

class Solution {
public int[][] solution(int[][] matrix_A, int[][] matrix_B) {
int[][] answer = new int[matrix_A.length][matrix_A[0].length];
for(int i = 0; i < answer.length; i++){
for(int j = 0; j < answer[0].length; j++){
answer[i][j] = matrix_A[i][j] + matrix_B[i][j];
}
}
return answer;
}
}
// 기존 코드 answer[i][j] = matrix_A[i][j];
7번 문제 - 빈칸 채우기

class Solution {
public int func1(int weight){
if(weight < 5){
return 500;
}
else if(weight < 10){
return 1000;
}
else{
return 4000;
}
}
public int func2(int w, int l, int h, int we){
if(w >= 80 || l >= 80 || h >= 80){
return -1;
}
if(w+l+h >= 160){
return -1;
}
if(we >= 25){
return -1;
}
return 0;
}
public int func3(int size){
if(size < 80){
return 3500;
}
else if(size < 100){
return 4500;
}
else if(size < 120){
return 6000;
}
else{
return 12000;
}
}
public int solution(int width, int length, int height, int weight) {
if(func2(width, length, height, weight) != 0){
return -1;
}
int price = func1(weight);
price += func3(width+length+height);
return price;
}
}
8번 문제 - 디버깅

class Solution {
public String solution(String key, String original) {
String answer = "";
for(int i = 0; i < original.length(); i++){
if(original.charAt(i) == ' '){
answer += " ";
continue;
}
int numOriginal = (int)original.charAt(i) - (int)'a';
int numKey = (int)key.charAt(i % key.length()) - (int)'a';
int numCryptogram = (numOriginal + numKey) % 26;
answer += (char)(numCryptogram + (int)'a');
}
return answer;
}
}
// 기존 코드 int numKey = (int)key.charAt(i) - (int)'a';
// original의 문자열 내용과 key의 길이가 다르면 StringIndexOutOfBoundsException 예외가 발생
9번 문제 - 코드 작성


1. 공부한 시간 합의 최댓값을 저장할 정수 max_time을 만들고 0을 저장합니다.
2. i를 0부터 times의 길이 - n 까지 1씩 증가시키며 아래 과정을 반복합니다.
2-1. times[i]부터 n일동안 공부한 시간 합을 저장할 정수 val_sum을 만들고 0을 저장합니다.
2-2. j를 i부터 i + n - 1 까지 1씩 증가시키며 아래 과정을 반복합니다.
2-2-1. val_sum에 times[j]를 더합니다.
2-3. 만약 max_time보다 val_sum이 더 크다면 max_time에 val_sum의 값을 저장합니다.
3. max_time을 return합니다.
class Solution {
public int solution(int[] times, int n) {
int max_time = 0;
for (int i=0; i <= times.length - n; i++) {
int val_sum = 0;
for (int j=i; j <= i+n-1; j++) {
val_sum += times[j];
}
if (max_time < val_sum) {
max_time = val_sum;
}
}
return max_time;
}
}
10번 문제 - 코드 작성

import java.util.*;
class Solution {
public String solution(String[][] snippet, String message) {
Map<String, String> s = new HashMap<>();
for (String[] p : snippet) { // Map에 key-value 형태로 저장
s.put(p[0], p[1]);
}
String[] words = message.split(" "); // 띄어쓰기를 구분자로 message를 분리하여 저장
StringBuilder answer = new StringBuilder();
for (String word : words) {
if (s.containsKey(word)) { // Map에 줄임말이 있는 경우
answer.append(s.get(word)).append(" "); // 해당 단어를 번환된 단어로 추가
} else {
answer.append(word).append(" "); // 원래 단어를 그대로 추가
}
}
return answer.toString().trim();
}
}
후기
샘플문제인 모의고사를 풀이해보았는데
빈칸 채우기 4개, 디버깅 4개, 코드 작성 2개로 총 10문제가 제출되었고
시험 시간은 50분이다.
PCCE는 필수역량인증이라 그런지 문제가 대체로 간단했다.
빠른 시일 내에 PCCP도 풀어보고 시험에 도전해보는 것도 좋을 것 같다.
PCSQL도 새로 생겼다고 하는데 나중에 도전해보고 싶다.
'알고리즘 & 코딩 테스트 > [Java]프로그래머스' 카테고리의 다른 글
| [프로그래머스_Java] Lv.0 잘라서 배열로 저장하기 (0) | 2024.01.18 |
|---|---|
| [프로그래머스_Java] Lv.0 조건에 맞게 수열 변환하기 2 (0) | 2024.01.09 |
| [프로그래머스_Java] Lv.0 qr code (1) | 2024.01.08 |
| [프로그래머스_Java] Lv.0 커피 심부름 (1) | 2024.01.08 |
| [프로그래머스_Java] Lv.0 이진수 더하기 (0) | 2024.01.05 |