[스터디] Java 프로그래머스 기초 30문제 (25.02.14)

2025. 2. 14. 17:32·LG 유플러스 유레카 SW/스터디

1. 수 조작하기 1

class Solution {
    public int solution(int n, String control) {
        
        for(String c:control.split("")) {
            switch(c) {
                case "w": 
                    n += 1;
                    break;
                case "s":
                    n -= 1;
                    break;
                case "d":
                    n += 10;
                    break;
                case "a":
                    n -= 10;
                    break;
                default:
                    break;
            }
        }
        return n;
    }
}

2. 마지막 두 원소

class Solution {
    public int[] solution(int[] num_list) {
        int[] answer = new int[num_list.length + 1];
        int l = num_list.length;
        
        for(int i = 0; i < l; i++) {
            answer[i] = num_list[i];
        }

        if(num_list[l - 1] > num_list[l - 2]) {
            answer[answer.length - 1] = num_list[l - 1] - num_list[l - 2];
        } else {
            answer[answer.length - 1] = num_list[l - 1] * 2;
        }
        return answer;
    }
}

3. 원소들의 곱과 합

class Solution {
    public int solution(int[] num_list) {
        int a = 1;
        int b = 0;
        
        for(int num: num_list) {
            a *= num;
            b += num;
        }
        
        return a < (b * b) ? 1 : 0; 
    }
}

4. 문자 리스트를 문자열로 변환하기

class Solution {
    public String solution(String[] arr) {
        return String.join("", arr);
    }
}

5. 문자열 돌리기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        
        for(String s: a.split("")){
            System.out.println(s);
        }
        
        sc.close();
    }
}

6. l로 만들기

class Solution {
    public String solution(String myString) {
        String answer = "";
        char ch = 'l';
         
        for(char str: myString.toCharArray()) {
            int numStr = (int)str;
            
            if(numStr > (int)ch) {
                answer += str;
            } else {
                answer += ch;
            }
        }
        
        return answer;
    }
}

7. 특별한 이차원 배열 1

class Solution {
    public int[][] solution(int n) {
        int[][] answer = new int[n][n];
        
        for(int i = 0; i < n; i++) {
            answer[i][i] = 1;
        }
        return answer;
    }
}

8. 간단한 식 계산하기

class Solution {
    public int solution(String binomial) {
        String[] str = binomial.split(" ");
        int a = Integer.parseInt(str[0]);
        int b = Integer.parseInt(str[2]);
        
        switch(str[1]) {
                case "+": return a + b;
                case "-": return a - b;
                case "*": return a * b;
                default: return 0;
        }

    
    }
}

9. 주사위 게임 1

class Solution {
    public int solution(int a, int b) {
        if(a % 2 != 0 && b % 2 != 0) return a * a + b * b;
        else if(a % 2 == 0 && b % 2 == 0) return Math.abs(a - b);
        else return 2 * (a + b);
    }
}

10. 조건에 맞게 수열 변환하기 3

class Solution {
    public int[] solution(int[] arr, int k) {
        if(k % 2 != 0) {
            for(int i = 0; i < arr.length; i++) {
                arr[i] *= k;
            }
        } else {
            for(int i = 0; i < arr.length; i++) {
                arr[i] += k;
            }
        }
        return arr;
    }
}

11. 9로 나눈 나머지

class Solution {
    public int solution(String number) {
        int answer = 0;
        
        for(String n: number.split("")) {
            answer += Integer.parseInt(n);
        }
        return answer % 9;
    }
}

12. 덧셈식 출력하기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        System.out.println(a + " + " + b + " = "+ (a + b));
    }
}

13. 배열의 원소 삭제하기

import java.util.*;

class Solution {
    public ArrayList<Integer> solution(int[] arr, int[] delete_list) {
        ArrayList<Integer> result = new ArrayList<>();
        ArrayList<Integer> dList = new ArrayList<>();
        
        for(int d: delete_list) {
            dList.add(d);
        }
        
        for(int a : arr) {
            if(!dList.contains(a)) {
                result.add(a);
            }
        }
        return result;
    }
}

14. 공백으로 구분하기 1

class Solution {
    public String[] solution(String my_string) {
        
        return my_string.split(" ");
    }
}

15. 부분 문자열

class Solution {
    public int solution(String str1, String str2) {
        return str2.contains(str1) ? 1 : 0;
    }
}

16. 꼬리 문자열

class Solution {
    public String solution(String[] str_list, String ex) {
        String answer = "";
        
        for(String str: str_list) {
            if(!str.contains(ex)) answer += str;
        }
        return answer;
    }
}

17. x 사이의 개수

import java.util.*;
class Solution {
    public ArrayList<Integer> solution(String myString) {
        
       ArrayList<Integer> a=new ArrayList<>();
        
        String[] arr=myString.split("x");
        
        for(int i=0;i<arr.length;i++){
            
            a.add(arr[i].length());
        }
        if(myString.substring(myString.length()-1).equals("x")){
            a.add(0);
        }
        return a;
    }
}

18. 0 떼기

class Solution {
    public String solution(String n_str) {
        return n_str.replaceFirst("^0+", "");
    }
}

19. 부분 문자열인지 확인하기

class Solution {
    public int solution(String my_string, String target) {
        return my_string.contains(target) ? 1: 0;
    }
}

20. 정수 찾기

class Solution {
    public int solution(int[] num_list, int n) {
        for(int num:num_list) {
            if(num == n) {
                return 1;
            }
        }
        return 0;
    }
}

21. 주사위 게임 2

class Solution {
    public int solution(int a, int b, int c) {
        if(a==b && b==c && a ==c){
            return (a+b+c)*(a*a+b*b+c*c)*(a*a*a+b*b*b+c*c*c);
        }else if(a!=b && b!=c &&a!=c){
            return a+b+c;
        }else{
            return (a+b+c)*(a*a+b*b+c*c);
        }
    }
}

22. 뒤에서 5등 위로

import java.util.*;

class Solution {
    public int[] solution(int[] num_list) {
        Arrays.sort(num_list);
        int[] result = Arrays.copyOfRange(num_list, 5, num_list.length);
        return result;
    }
}

23. 문자열 곱하기

class Solution {
    public String solution(String my_string, int k) {
        return my_string.repeat(k);
    }
}

24. 홀짝 구분하기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        if(n % 2 == 0) System.out.println(n + " is even");
        else System.out.println(n + " is odd");
    }
}

25. 이어 붙인 수

class Solution {
    public int solution(int[] num_list) {
        int answer = 0;
        String even = "";
        String odd = "";
        
        for(int n:num_list) {
            if(n % 2 == 0) {
                even += n;
            } else {
                odd += n;
            }
        }
        return Integer.parseInt(even) + Integer.parseInt(odd);
    }
}

26. 홀짝에 따라 다른 값 반환하기

class Solution {
    public int solution(int n) {
        int answer = 0;
        
        if(n % 2 != 0) {
            for(int i = 1; i <= n; i += 2) {
                answer += i;
            }
        } else {
            for(int i = 2; i <= n; i += 2) {
                answer += i * i;
            }
        }
        
        return answer;
    }
}

27. 문자열 붙여서 출력하기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        String b = sc.next();
        
        System.out.println(a + b);
    }
}

28. 특별한 이차원 배열 2

class Solution {
    public int solution(int[][] arr) {
        
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr.length; j++) {
                if(arr[i][j] != arr[j][i]) return 0;
            }
        }
        return 1;
    }
}

29. 더 크게 합치기

class Solution {
    public int solution(int a, int b) {
        
        String str1 = a + "" + b;
        String str2 = b + "" + a;
        
        return Integer.parseInt(str1) > Integer.parseInt(str2) ? Integer.parseInt(str1) : Integer.parseInt(str2);
    
    }
}

30. n의 배수

class Solution {
    public int solution(int num, int n) {
        return num % n == 0 ? 1 : 0;
    }
}
728x90
반응형

'LG 유플러스 유레카 SW > 스터디' 카테고리의 다른 글

[스터디] Java 도전 문제 3문제 (25.02.20)  (0) 2025.02.20
[스터디] Java 프로그래머스 PCCE 기출 14문제 (25.02.19)  (1) 2025.02.19
[스터디] Java 프로그래머스 기초 24문제 & 머쓱이 획득 (25.02.18)  (1) 2025.02.18
[스터디] Java 프로그래머스 기초 30문제 (25.02.17)  (0) 2025.02.17
[스터디] Java 프로그래머스 기초 40문제 (25.02.13)  (1) 2025.02.13
'LG 유플러스 유레카 SW/스터디' 카테고리의 다른 글
  • [스터디] Java 프로그래머스 PCCE 기출 14문제 (25.02.19)
  • [스터디] Java 프로그래머스 기초 24문제 & 머쓱이 획득 (25.02.18)
  • [스터디] Java 프로그래머스 기초 30문제 (25.02.17)
  • [스터디] Java 프로그래머스 기초 40문제 (25.02.13)
nueos
nueos
  • nueos
    nueos 공부 기록
    nueos
  • 전체
    오늘
    어제
    • 분류 전체보기 (193)
      • 해커톤 (1)
      • 네이버 BoostCamp (6)
      • LG 유플러스 유레카 SW (5)
        • React (21)
        • TypeScript (2)
        • JavaScript (2)
        • HTML+CSS (5)
        • Spring (7)
        • Java (6)
        • SQL (2)
        • Algorithm (8)
        • CX (6)
        • Git (2)
        • 프로젝트 (2)
        • 스터디 (9)
        • 과제 (8)
        • 특강 (1)
      • React (3)
      • Next (0)
      • Javascript (2)
      • HTML (2)
      • CSS (9)
      • Algorithm (6)
      • Database (0)
      • OS (13)
      • C++ (24)
      • Python (1)
      • jQuery (1)
      • Django (1)
      • Git (1)
      • 개발 지식 (3)
      • 정보 보안 (22)
      • 포렌식 (1)
      • 암호 (2)
      • 기타 (4)
      • 패스트캠퍼스 FE 프로젝트십 (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    제주해커톤
    디지털혁신
    완전 탐색
    큐
    heap
    기술로바꾸는세상
    디지랩챌린지
    Queue
    제주지역혁신플랫폼지능형서비스사업단
    exhaustive search
    Stack
    힙
    스택
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
nueos
[스터디] Java 프로그래머스 기초 30문제 (25.02.14)
상단으로

티스토리툴바