프로그래머스5일차

지선·2023년 5월 1일

코딩테스트

목록 보기
5/8

외계행성의 나이

class Solution {
    public String solution(int age) {
        String answer = "";
        //age가 정수형이므로, 문자형으로 바꿔준다.
        String temp=Integer.toString(age);
        temp=temp.replace("0","a");
        temp=temp.replace("1","b");
        temp=temp.replace("2","c");
        temp=temp.replace("3","d");
        temp=temp.replace("4","e");
        temp=temp.replace("5","f");
        temp=temp.replace("6","g");
        temp=temp.replace("7","h");
        temp=temp.replace("8","i");
        temp=temp.replace("9","j");
        answer=temp;
        return answer;
    }
}

진료순서 정하기(해결중..)

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

무엇이 문젠지 모르겠지만.. 자꾸 실행 결과가 제대로 나오지 않는다..ㅠㅠ

n의 배수 고르기

class Solution {
    public int[] solution(int n, int[] numlist) {
        int[] answer=null;
        int[] temp=new int[100001];
        int cnt=0;
        for(int i=0;i<numlist.length;i++){
            if(numlist[i]%n==0){
                temp[cnt]=numlist[i];
                cnt++;
            }
        }
        answer=new int[cnt];
        for(int i=0;i<cnt;i++){
            answer[i]=temp[i];
        }
        
        return answer;
    }
}

인덱스바꾸기

class Solution {
    public String solution(String my_string, int num1, int num2) {
        String answer = "";
        char [] ch= my_string.toCharArray();
       
        char temp=ch[num1];
        ch[num1]=ch[num2];
        ch[num2]=temp;
        for(int i=0;i<my_string.length();i++){
            answer+=ch[i];
        }
        return answer;
    }
}

가장 큰 수 찾기

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

월요일이랑 금요일은 조금밖에 풀지 못할 것 같아요.. 알바하고 와서 너무 피곤해요ㅜ
그럼 모두 화이팅하세요!

profile
긍정왕되기

0개의 댓글