📌Back-End/프로그래머스
Java 프로그래머스 코딩테스트 연습 [PCCE 기출문제] 1 ~ 5번
구 일
2024. 7. 22. 21:17
728x90
반응형
출력
/**
* 코딩테스트 연습 PCCE 기출문제 1번
* 출력
*/
public class Main0722_1 {
public static void main(String[] args) {
String msg = "Spring is beginning";
int val1 = 3;
String val2 = "3";
System.out.println(msg);
System.out.println(val1 + 10);
System.out.println(val2 + "10");
}
}
피타고라스의 정리
import java.util.Scanner;
/**
* 코딩테스트 연습 PCCE 기출문제 2번
* 피타고라스의 정리
*/
public class Main0722_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int c = sc.nextInt();
int b_square = (int) Math.pow(c, 2) - (int) Math.pow(a, 2);
System.out.println(b_square);
}
}
나이 계산
import java.util.Scanner;
/**
* 코딩테스트 연습 PCCE 기출문제 3번
* 나이 계산
*/
public class Main0722_3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
String age_type = sc.next();
int answer = 0;
if (age_type.equals("Korea")) {
answer = 2030 - year + 1;
} else if (age_type.equals("Year")) {
answer = 2030 - year;
}
System.out.println(answer);
}
}
저축
import java.util.Scanner;
/**
* 코딩테스트 연습 PCCE 기출문제 4번
* 저축
*/
public class Main0722_4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int start = sc.nextInt();
int before = sc.nextInt();
int after = sc.nextInt();
int money = start;
int month = 1;
while (money < 70) {
money += before;
month++;
}
while (money < 100) {
money += after;
month++;
}
System.out.println(month);
}
}
산책
/**
* 코딩테스트 연습 PCCE 기출문제 5번
* 산책
*/
public class Main0722_5 {
public static void main(String[] args) {
int[] result = solution("NSSNEWWN");
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
result = solution("EESEEWNWSNWWNS");
for (int num : result) {
System.out.print(num + " ");
}
System.out.println();
}
public static int[] solution(String route) {
int east = 0;
int north = 0;
int[] answer = new int[2];
for (int i = 0; i < route.length(); i++) {
switch (route.charAt(i)) {
case 'N':
north++;
break;
case 'S':
north--;
break;
case 'E':
east++;
break;
case 'W':
east--;
break;
}
}
answer[0] = east;
answer[1] = north;
return answer;
}
}
728x90
반응형