๐ช๐ป 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์ง๋ฒ ์ ์๋ก ๋ณํํ๋ ์์
์ ํจ.