1. 산책
class Solution {
public 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;
}
}
2. 저축
import java.util.Scanner;
public class Solution {
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);
}
}
3. 병과 분류
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String code = sc.next();
String lastFourWords = code.substring(code.length()-4, code.length());
if(lastFourWords.equals("_eye")){
System.out.println("Ophthalmologyc");
}
else if(lastFourWords.equals( "head" )){
System.out.println("Neurosurgery");
}
else if(lastFourWords.equals( "infl" )){
System.out.println("Orthopedics");
}
else if(lastFourWords.equals( "skin" )){
System.out.println("Dermatology");
}
else{
System.out.println("direct recommendation");
}
}
}
4. 피타고라스의 정리
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int c = sc.nextInt();
int b_square = c * c - a * a;
System.out.println(b_square);
}
}
5. 나이 계산
import java.util.Scanner;
public class Solution {
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);
}
}
6. 가습기
class Solution {
public int func1(int humidity, int val_set){
if(humidity < val_set)
return 3;
return 1;
}
public int func2(int humidity){
if(humidity >= 50)
return 0;
else if (humidity >= 40)
return 1;
else if (humidity >= 30)
return 2;
else if (humidity >= 20)
return 3;
else if (humidity >= 10)
return 4;
else
return 5;
}
public int func3(int humidity, int val_set){
if(humidity < val_set)
return 1;
return 0;
}
public int solution(String mode_type, int humidity, int val_set) {
int answer = 0;
if(mode_type.equals("auto")){
answer = func2(humidity);
}
else if(mode_type.equals("target")){
answer = func1(humidity, val_set);
}
else if(mode_type.equals("minimum")){
answer = func3(humidity, val_set);
}
return answer;
}
}
7. 각도 합치기
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int angle1 = sc.nextInt();
int angle2 = sc.nextInt();
int sum_angle = angle1 + angle2;
System.out.println(sum_angle % 360);
}
}
8. 심폐소생술
class Solution {
public int[] solution(String[] cpr) {
int[] answer = {0, 0, 0, 0, 0};
String[] basic_order = {"check", "call", "pressure", "respiration", "repeat"};
for(int i=0; i<cpr.length; i++){
for(int j=0; j<basic_order.length; j++){
if(cpr[i].equals(basic_order[j])){
answer[i] = j + 1;
break;
}
}
}
return answer;
}
}
9. 닉네임 규칙
class Solution {
public String solution(String nickname) {
String answer = "";
for(int i=0; i<nickname.length(); i++){
if(nickname.charAt(i) == 'l'){
answer += "I";
}
else if(nickname.charAt(i) == 'w'){
answer += "vv";
}
else if(nickname.charAt(i) == 'W'){
answer += "VV";
}
else if(nickname.charAt(i) == 'O'){
answer += "0";
}
else{
answer += nickname.charAt(i);
}
}
while(answer.length() < 4){
answer += "o";
}
if(answer.length() > 8){
answer = answer.substring(0, 8);
}
return answer;
}
}
10. 지폐 접기
class Solution {
public int solution(int[] wallet, int[] bill) {
int answer = 0;
int wMin = Math.min(wallet[0], wallet[1]);
int bMin = Math.min(bill[0], bill[1]);
int wMax = Math.max(wallet[0], wallet[1]);
int bMax = Math.max(bill[0], bill[1]);
while(bMin > wMin || bMax > wMax) {
if(bill[0] > bill[1]) {
bill[0] /= 2;
} else {
bill[1] /= 2;
}
bMin = Math.min(bill[0], bill[1]);
bMax = Math.max(bill[0], bill[1]);
answer++;
}
return answer;
}
}
11. 버스
class Solution {
public int solution(int seat, String[][] passengers) {
int num_passenger = 0;
for(int i=0; i<passengers.length; i++){
num_passenger += func4(passengers[i]);
num_passenger -= func3(passengers[i]);
}
int answer = func1(seat - num_passenger) ;
return answer;
}
public int func1(int num){
if(0 > num){
return 0;
}
else{
return num;
}
}
public int func2(int num){
if(num > 0){
return 0;
}
else{
return num;
}
}
public int func3(String[] station){
int num = 0;
for(int i=0; i<station.length; i++){
if(station[i].equals("Off")){
num += 1;
}
}
return num;
}
public int func4(String[] station){
int num = 0;
for(int i=0; i<station.length; i++){
if(station[i].equals("On")){
num += 1;
}
}
return num;
}
}
12. 이웃한 칸
class Solution {
public int solution(String[][] board, int h, int w) {
int answer = 0;
int n = board.length;
int count = 0;
int[] dh = {0, 1, -1, 0}, dw = {1, 0, 0, -1};
for(int i = 0; i <= 3; i++) {
int h_check = h + dh[i];
int w_check = w + dw[i];
if(h_check >= 0 && h_check < n && w_check >= 0 && w_check < n) {
if(board[h][w].equals(board[h_check][w_check])) {
count++;
}
}
}
return count;
}
}
13. 데이터 분석
import java.util.*;
class Solution {
public int[][] solution(int[][] data, String ext, int val_ext, String sort_by) {
int[][] answer = {};
Map<String, Integer> map = new HashMap<>();
map.put("code", 0);
map.put("date", 1);
map.put("maximum", 2);
map.put("remain", 3);
System.out.println(map);
int[][] arr = Arrays.stream(data).filter(t -> t[map.get(ext)] < val_ext).toArray(int[][]::new);
Arrays.sort(arr, (o1, o2) -> {
return o1[map.get(sort_by)] - o2[map.get(sort_by)];
});
return arr;
}
}
14. 공원
import java.util.*;
class Solution {
static String[][] park;
static boolean check(int x, int y, int m) {
for(int i = 0; i < m; i++) {
for(int j = 0; j < m; j++) {
if(x + i >= park.length || y + j >= park[0].length) {
return false;
}
if(!park[x+i][y+j].equals("-1")) {
return false;
}
}
}
return true;
}
public int solution(int[] mats, String[][] inputPark) {
park = inputPark;
int maxSize = -1;
Arrays.sort(mats);
for(int i = 0; i < park.length; i++){
for(int j = 0; j < park[i].length; j++) {
if(park[i][j].equals("-1")) {
// 매트 사이즈를 돌면서 가능한 지 체크
for(int m:mats) {
if(check(i, j, m)) {
maxSize = Math.max(m, maxSize);
}
}
}
}
}
return maxSize;
}
}
728x90
반응형
'LG 유플러스 유레카 SW > 스터디' 카테고리의 다른 글
[스터디] Java 알고리즘 2문제 (25.02.21) (1) | 2025.02.21 |
---|---|
[스터디] Java 도전 문제 3문제 (25.02.20) (0) | 2025.02.20 |
[스터디] Java 프로그래머스 기초 24문제 & 머쓱이 획득 (25.02.18) (1) | 2025.02.18 |
[스터디] Java 프로그래머스 기초 30문제 (25.02.17) (0) | 2025.02.17 |
[스터디] Java 프로그래머스 기초 30문제 (25.02.14) (1) | 2025.02.14 |