본문 바로가기

혼자 코딩 공부하기/프로그래머스 코딩테스트

231216 프로그래머스 자바 0단계 다 풀어보기

개미 군단

class Solution {
    public int solution(int hp) {
        int answer = 0;
        int less = 0;
        answer += hp / 5;
        less += hp % 5;
        answer += less / 3;
        less %= 3;
        answer += less / 1;
        return answer;
    }
}

 

세균 증식

class Solution {
    public int solution(int n, int t) {
        int answer = n;
        for(int i = 0; i < t; i++) {
            answer *= 2; 
        }
        return answer;
    }
}

 

n 의 배수 고르기

class Solution {
    public int[] solution(int n, int[] numlist) {
        int[] answer = {};
        int count = 0;
        for (int i : numlist) {
            if (i % n == 0) {
                count++;
            }
        }
        answer = new int[count];
        count = 0;
        for(int i : numlist) {
            if (i % n == 0) {
                answer[count] = i;
                count++;
            }
        }
        return answer;
    }
}

 

대문자와 소문자

class Solution {
    public String solution(String my_string) {
        String answer = "";
        for(int i = 0; i < my_string.length();i++) {
            if (Character.isLowerCase(my_string.charAt(i))) {
                answer += Character.toUpperCase(my_string.charAt(i));
            } else {
                answer += Character.toLowerCase(my_string.charAt(i));
            }
        }
        return answer;
    }
}

 

가위 바위 보

class Solution {
    public String solution(String rsp) {
        String answer = "";
        for (int i = 0; i < rsp.length();i++) {
            if (rsp.charAt(i) == '2') {
                answer += "0";
            } else if (rsp.charAt(i) == '0') {
                answer += "5";
            } else {
                answer += "2";
            }
        }
        return answer;
    }
}

 

배열의 원소 삭제하기

class Solution {
    public int[] solution(int[] arr, int[] delete_list) {
        int[] answer = {};
        int count = 0;
        int temp = arr.length;
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < delete_list.length; j++){
                if (arr[i] == delete_list[j]) {
                    temp--;
                    arr[i] = 0;
                }
            }
        }
        answer = new int[temp];
        for (int i : arr) {
            if (i != 0) {
                answer[count] = i;
                count++;
            }
        }
        return answer;
    }
}

 

암호 해독

class Solution {
    public String solution(String cipher, int code) {
        String answer = "";        
        for(int i = code - 1; i < cipher.length(); i += code) {
            answer += cipher.charAt(i);
        }
        return answer;
    }
}

 

주사위의 개수

class Solution {
    public int solution(int[] box, int n) {
        int answer = 1;
        for (int i : box) {
            answer *= i / n;
        }
        return answer;
    }
}

 

최댓값 만들기 (2)

import java.util.*;

class Solution {
    public int solution(int[] numbers) {
        int answer = 0;
        Arrays.sort(numbers);
        if (numbers[0] * numbers[1] > numbers[numbers.length-2] * numbers[numbers.length - 1]) {
            answer = numbers[0] * numbers[1];
        } else {
            answer = numbers[numbers.length - 1] * numbers[numbers.length - 2];
        }
        return answer;
    }
}

 

직각삼각형 만들기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i = 0; i < n; i++) {
            for(int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
//        System.out.println(n);
    }
}

 

가장 큰 수 찾기

class Solution {
    public int[] solution(int[] array) {
        int[] answer = new int[2];
        int number = array[0];
        int idx = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] > number) {
                number = array[i];
                idx = i;
            }
        }
        answer[0] = number;
        answer[1] = idx;
        return answer;
    }
}

 

특이한 이차원 배열 1

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

 

배열 회전시키기

class Solution {
    public int[] solution(int[] numbers, String direction) {
        int[] answer = new int[numbers.length];
        switch (direction) {
            case "right" :
                answer[0] = numbers[numbers.length - 1];
                for(int i = 0, j = 1; j < answer.length; i++, j++) {
                    answer[j] = numbers[i];
                }
                break;
            case "left" :
                answer[answer.length - 1] = numbers[0];
                for(int i = 0, j = 1; j < answer.length; i++, j++) {
                    answer[i] = numbers[j];
                }
                break;
        }
        return answer;
    }
}

 

배열 만들기 3

class Solution {
    public int[] solution(int[] arr, int[][] intervals) {
        int[] answer = {};
        int num = 0;
        for(int i = 0; i < intervals.length; i++) {
            num += intervals[i][1] - intervals[i][0] + 1;
        }
        answer = new int[num];
        num = 0;
        for(int i = 0; i < intervals.length; i++) {
            for(int j = intervals[i][0]; j < intervals[i][1] + 1; j++) {
                answer[num] = arr[j];
                num++;
            }
        }
        return answer;
    }
}

 

외계행성의 나이

class Solution {
    public String solution(int age) {
        String answer = "";
        String age1 = Integer.toString(age);
        for(int i = 0; i < age1.length(); i++) {
            answer += Character.toString(age1.charAt(i) + 49);
        }
        return answer;
    }
}

 

약수 구하기

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

 

피자 나눠 먹기 (2)

class Solution {
    public int solution(int n) {
        int answer = 0;
        int i = 1;
        while (true) {
            if (n * i % 6 == 0) {
                answer = n * i / 6;
                break;
            }
            i++;
        }
        return answer;
    }
}

 

숫자 찾기

class Solution {
    public int solution(int num, int k) {
        int answer = -1;
        if (Integer.toString(num).contains(Integer.toString(k))){
            answer = Integer.toString(num).indexOf(Integer.toString(k)) + 1;
        }
        return answer;
    }
}

 

369 게임

class Solution {
    public int solution(int order) {
        int answer = 0;
        for (int i = 0; i < String.valueOf(order).length(); i++) {
            if ((String.valueOf(order).charAt(i) - '0') % 3 == 0 
            	&& String.valueOf(order).charAt(i) != '0') {
                answer++;
            }
        }
        return answer;
    }
}

 

문자열 정렬하기

import java.util.*;

class Solution {
    public String solution(String my_string) {
        String answer = "";
        char[] c = my_string.toLowerCase().toCharArray();
        Arrays.sort(c);
        for(char cc : c) {
            answer += cc;
        }
        return answer;
    }
}