Post

๐Ÿ’ช๐Ÿป programmers Lv1(24.09.26 - 5๋ฌธ์ œ)

๐Ÿ’ช๐Ÿป programmers Lv1(24.09.26 - 5๋ฌธ์ œ)

programmers Lv1(24.09.26 - 5๋ฌธ์ œ)

31) ๊ฐ™์€ ์ˆซ์ž๋Š” ์‹ซ์–ด

  • ๋ฐฐ์—ด arr, ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋Š” ์ˆซ์ž 0๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ด๋ฃจ์–ด์ ธ ์žˆ์Œ. ๋ฐฐ์—ด arr์—์„œ ์—ฐ์†์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ์ˆซ์ž๋Š” ํ•˜๋‚˜๋งŒ ๋‚จ๊ธฐ๊ณ  ์ „๋ถ€ ์ œ๊ฑฐ.

  • ์—ฐ์†์ ์ธ ์ˆซ์ž => Stack
  • ์Šคํƒ ์ƒ์„ฑ ๋ฐ ์„ ์–ธ => Stack<Integer> st = new Stack<>();

  • stack ์‹์ด ๊ธฐ์–ต๋‚˜์ง€ ์•Š์•„ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.*;

public class Solution {
    public int[] solution(int []arr) {
        int[] answer;
        Stack<Integer> st = new Stack<>(); //์Šคํƒ ์ƒ์„ฑ ๋ฐ ์„ ์–ธ
        st.push(arr[0]); // 1
        for(int i=0;i<arr.length;i++)
        {
            if(st.peek()==arr[i])
            {
                continue;
            }
            else
            {
                st.push(arr[i]);
            }
        }
        answer = new int[st.size()]; // 4
    
        for(int i=st.size()-1;i>=0;i--)
        {
            answer[i] = st.pop();
        }

        return answer;
    }
}
  • List๋กœ ํ’€์–ด๋ณด๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.*;

public class Solution {
    public int[] solution(int []arr) {
        int[] answer;
        List<Integer> li = new ArrayList<Integer>();
        li.add(arr[0]);
        
        for(int i=1;i<arr.length;i++)
        {
            if(arr[i]!=arr[i-1])
            {
                li.add(arr[i]);
            }
        }
        answer = new int[li.size()];
        
        for(int i=0;i<li.size();i++)
        {
            answer[i] = li.get(i);
        }
        
        return answer;
    }
}



32) [PCCE ๊ธฐ์ถœ๋ฌธ์ œ] 4๋ฒˆ / ๋ณ‘๊ณผ๋ถ„๋ฅ˜

  • ๋นˆ์นธ ์ฑ„์šฐ๊ธฐ ๋ฌธ์ œ

  • java์—์„œ ๋ฌธ์ž์—ด ๋น„๊ต๋Š” โ€œ==โ€ ์—ฐ์‚ฐ์ž๊ฐ€ ์•„๋‹Œ .equals()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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");
        }
    }
}



33) [PCCE ๊ธฐ์ถœ๋ฌธ์ œ] 2๋ฒˆ / ๊ฐ๋„ ํ•ฉ์น˜๊ธฐ

  • ๋””๋ฒ„๊น… ๋ฌธ์ œ, ์ด๋ฏธ ์™„์„ฑ๋œ ์ฝ”๋“œ์—์„œ ๋ฒ„๊ทธ๋ฅผ ์ˆ˜์ •ํ•˜๊ธฐ

  • ์–ด๋– ํ•œ ๊ฐ๋„๋ฅผ ๋”ํ•ด๋„ 360๋„ ๋ฏธ๋งŒ์œผ๋กœ ์ถœ๋ ฅํ•˜๊ธฐ, ์ฒซ ๊ฐ๋„์™€ ๋‘ ๋ฒˆ์งธ ๊ฐ๋„๋ฅผ ๋”ํ•ด์„œ 360๋„์˜ ๋‚˜๋จธ์ง€ ๊ฐ’์ด ๋‹ต

1
2
3
4
5
6
7
8
9
10
11
12
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)%360;
        System.out.println(sum_angle);
    }
}



34) ์˜ˆ์‚ฐ

  • ๋ถ€์„œ๋ณ„๋กœ ์‹ ์ฒญํ•œ ๊ธˆ์•ก์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด d์™€ ์˜ˆ์‚ฐ budget์ด ๋งค๊ฐœ๋ณ€์ˆ˜, ์ตœ๋Œ€ ๋ช‡ ๊ฐœ์˜ ๋ถ€์„œ์— ๋ฌผํ’ˆ์„ ์ง€์›ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๊ตฌํ•˜๊ธฐ

  • ๐Ÿ“Œ ๋‚ด ํ’€์ด

    • 1) d[] ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

    • 2) d ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต, if๋ฌธ์„ ํ†ตํ•ด d[i]์˜ ํ•ฉ์ด budget๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ์˜ ํšŸ์ˆ˜ ๊ตฌํ•˜๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.*;
class Solution {
    public int solution(int[] d, int budget) {
        int answer = 0;
        Arrays.sort(d); // ์˜ค๋ฆ„์ฐจ์ˆœ
        int sum = 0, cnt = 0;
        for(int i=0;i<d.length;i++)
        {
            if((sum+=d[i])<=budget)
            {
                cnt++;
            }
        }
        return cnt;
    }
}



35) 3์ง„๋ฒ• ๋’ค์ง‘๊ธฐ

  • n์„ 3์ง„๋ฒ• ์ƒ์—์„œ ์•ž๋’ค๋กœ ๋’ค์ง‘์€ ํ›„, ์ด๋ฅผ ๋‹ค์‹œ 10์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„ํ•œ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

  • ๐Ÿ“Œ ๋‚ด ํ’€์ด

    • 1) 10์ง„์ˆ˜๋ฅผ 3์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ -> Integer.toString(n, 3)

    • 2) 3์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•œ ์ˆ˜๋ฅผ charAt()์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๊ธฐ

    • 3) ๋ฌธ์ž์—ด์„ 3์ง„๋ฒ•์œผ๋กœ ํ•ด์„ํ•ด์„œ 10์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ -> Integer.parseInt(str, 3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
class Solution {
    public int solution(int n) {
        int answer = 0;
        String s = Integer.toString(n, 3); // 10์ง„์ˆ˜ -> 3์ง„๋ฒ•
        String str = "";
        for(int i=s.length()-1;i>=0;i--)
        {
            str += s.charAt(i);
        }
        answer = Integer.parseInt(str, 3); // str์„ 3์ง„๋ฒ•์œผ๋กœ ํ•ด์„ํ•ด์„œ 10์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜
        return answer;
    }
}

Integer.parseInt(String s, int radix) ๋ฉ”์„œ๋“œ

์ด ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด s๋ฅผ radix(๊ธฐ์ˆ˜, ์ฆ‰ ์ง„๋ฒ•)์— ๋”ฐ๋ผ ํ•ด์„ํ•ด์„œ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•จ. ๋‘ ๋ฒˆ์งธ ์ธ์ž์ธ radix๋Š” ๊ทธ ๋ฌธ์ž์—ด์ด ์–ด๋–ค ์ง„๋ฒ•์œผ๋กœ ํ‘œํ˜„๋œ ๊ฒƒ์ธ์ง€๋ฅผ ์•Œ๋ ค์คŒ.

์ฝ”๋“œ์—์„œ Integer.parseInt(str, 3)์˜ ์—ญํ• 

์ด ์ฝ”๋“œ๋Š” ๋ฌธ์ž์—ด str์„ 3์ง„๋ฒ•์œผ๋กœ ํ•ด์„ํ•ด์„œ 10์ง„๋ฒ• ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์„ ํ•จ.

This post is licensed under CC BY 4.0 by the author.