๐ช๐ป programmers Lv1(24.09.24 - 10๋ฌธ์ )
programmers Lv1(24.09.24 - 10๋ฌธ์ )
11) ์ ์ ์ ๊ณฑ๊ทผ ํ๋ณ
์ ์ n, n์ด ์ ์ x์ ์ ๊ณฑ์ธ์ง ํ๋จ
1) ์ ๊ณฑ๊ทผ ๊ตฌํ๊ธฐ ->
Math.sqrt()
2) ์์์ ๋ฒ๋ฆฌ๊ณ
long ํ์
์ผ๋ก ๋ณํ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
class Solution {
public long solution(long n) {
long answer = 0;
double sqrt = Math.sqrt(n); // ์ ๊ณฑ๊ทผ ๊ตฌํ๊ธฐ
long x = (long)sqrt; // ์์์ ๋ฒ๋ฆฌ๊ณ ํ์
๋ณํ
if(x*x == n)
{
answer = (x+1)*(x+1);
}
else
{
answer = -1;
}
return answer;
}
}
12) ํ์ค๋ ์
ํ์ค๋ ์์ด๋ ค๋ฉด x์ ์๋ฆฟ์ ํฉ์ผ๋ก x๊ฐ ๋๋์ด์ ธ์ผ ํจ.
ex) 18 => 1 + 8 = 9, 18 /9 = 2 (ํ์ค๋ ์)
1) ๊ฐ ์๋ฆฌ์ ๊ตฌํ๊ณ ๋ํ๊ธฐ
2) x๋ฅผ ๊ตฌํ ๊ฐ์ผ๋ก ๋๋์์ ๋ ๋๋์ด ๋จ์ด์ง๋ฉด ํ์ค๋ ์, ์๋๋ฉด ํ์ค๋ ์๊ฐ ์๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
class Solution {
public boolean solution(int x) {
boolean answer = true;
int sum = 0;
int originX = x;
while(x!=0)
{
sum += (x%10);
x /= 10;
}
if(originX%sum == 0)
{
return answer;
}
else
{
return !answer;
}
}
}
- โ ์ฒ์ ํ๋ ธ๋ ๋ถ๋ถ : originX๋ผ๋ ๋ณ์๋ฅผ ๋ฐ๋ก ๋์ง ์๊ณ x%sum == 0 ์ผ๋ก ํ์๋๋ฐ ๊ทธ๋ฌ๋ฉด ์ด๋ฏธ x๊ฐ 0์ด ๋ ์์ ์ด๋ฏ๋ก ๊ฒฐ๊ณผ์ ์ค๋ฅ๊ฐ ์๊ฒผ์.. ๋ฐ๋ณด. ์ฆ, ์๋์ x๋ก ๋๋์์ ๋๋ฅผ ๊ตฌํด์ผ๋๋ฏ๋ก x๋ฅผ originX๋ผ๋ ๋ณ์๋ก ์ ์ฅํด ๋๊ณ ํด๋น originX ๋ณ์๋ก ๋๋์์.
13) ๋ ์ ์ ์ฌ์ด์ ํฉ
๋ ์ ์ a, b๊ฐ ์ฃผ์ด์ก์ ๋ a์ b ์ฌ์ด์ ์ํ ๋ชจ๋ ์ ์์ ํฉ ๊ตฌํ๊ธฐ
1) a๋ถํฐ ์์ํด์ b๊น์ง ์์ ํฉ ๊ตฌํ๊ธฐ
2) b๋ถํฐ ์์ํด์ a๊น์ง ์์ ํฉ ๊ตฌํ๊ธฐ
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
class Solution {
public long solution(int a, int b) {
long answer = 0;
for(int i=a;i<=b;i++)
{
if(a==b)
{
answer = a;
}
else{
answer +=i;
}
}
for(int i=b;i<=a;i++)
{
if(a==b)
{
answer = a;
}
else{
answer +=i;
}
}
return answer;
}
}
- ๋งค์ฐ ์คํ๊ฒํฐ,,.,,,์ฝ๋๋ก ๋ค๋ฅธ ์ฌ๋ ์ฝ๋๋ฅผ ์ฐพ์๋ณด๋ ์ผํญ์ฐ์ฐ์๋ฅผ ์ฐ๋ฉด ๋งค์ฐ ๊ฐ๋จํ๊ฒ ํด๊ฒฐ๋๋ ๊ฒ์ ํ์ธํ์๋ค. ๋ฐํ ๊ฐ์๋ ์ผํญ์ ๋ง์ด ์ผ์๋๋ฐ for๋ฌธ์๋ ์ ์ ์ผ์ด์ ์ฐ์ต์ ํด์ผ๋ ๊ฑฐ ๊ฐ๋ค.
1
2
3
4
5
6
7
8
9
class Solution {
public long solution(int a, int b) {
long answer = 0;
for (int i = ((a < b) ? a : b); i <= ((a < b) ? b : a); i++)
answer += i;
return answer;
}
}
14) ์์ธ์์ ๊น์๋ฐฉ ์ฐพ๊ธฐ
String ๋ฐฐ์ด seoul ์์ ์ค โKimโ์ ์์น x๋ฅผ ์ฐพ์ โ๊น์๋ฐฉ์ x์ ์๋คโ ๋ String์ ๋ฐํํ๋ ํจ์ ๊ตฌํ๊ธฐ
1) โKimโ์ด๋ผ๋ ๊ธ์๊ฐ ๋ฐฐ์ด์ ๋ช ๋ฒ์งธ์ ์์นํ๋์ง ๊ตฌํ๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
class Solution {
public String solution(String[] seoul) {
String answer = "";
int idx = 0;
for(int i=0;i<seoul.length;i++)
{
if(seoul[i].equals("Kim"))
{
idx = i;
break;
}
}
answer = "๊น์๋ฐฉ์ "+idx+"์ ์๋ค";
return answer;
}
}
โ ๊ณ์ ๋ง๊ฒ ํ๋๋ฐ ์ ์ ๋์ง ํ์๋๋ฐ ๋ java์์ ๋ฌธ์์ด ๋น๊ต์ธ๋ฐ == ์ ์ฌ์ฉํ๊ณ ๋ง์๋ค.
Java์์
โ==โ ์ฐ์ฐ์
๋ ๋ ๊ฐ์ ๋ฌธ์์ด์ด ๋ฉ๋ชจ๋ฆฌ ์์ ๋์ผํ ์์น์ ์๋์ง๋ฅผ ๋น๊ตํ๋ ๋ฐ๋ฉด,โequals()โ
๋ฉ์๋๋ ๋ ๋ฌธ์์ด์ ๋ด์ฉ์ด ๊ฐ์์ง๋ฅผ ๋น๊ตํจ.
15) ์ฝ๋ผ์ธ ์ถ์ธก
์ฝ๋ผ์ธ ์ถ์ธก์ ์ฃผ์ด์ง ์๊ฐ 1์ด ๋ ๋๊น์ง ๋ค์ ์์ ์ ๋ฐ๋ณตํ๋ฉด ๋ชจ๋ ์๋ฅผ 1๋ก ๋ง๋ค ์ ์๋ค๋ ์ถ์ธก
1) 1์ด๋ ๋๊น์ง ์ง์์ผ ๋ 2๋ก ๋๋๊ธฐ
2) 1์ด๋ ๋๊น์ง ํ์์ผ ๋ 3์ ๊ณฑํ๊ณ 1์ ๋ํ๊ธฐ
3) ์ด ๊ณผ์ ์ ๋ฐ๋ณตํด์ ํ์ ๊ตฌํ๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public int solution(int num) {
int cnt = 0;
int answer = 0;
while(num!=1)
{
if(num%2 == 0)
{
num = num/2;
}
else if(num%2 == 1)
{
num = num*3 + 1;
}
cnt++;
if(cnt>=500)
{
return -1;
}
}
return cnt;
}
}
16) ์์ ๋ํ๊ธฐ
์ ์๋ค์ ์ ๋๊ฐ์ ์ฐจ๋ก๋ก ๋ด์ ์ ์ ๋ฐฐ์ด absolutes, ์ ์๋ค์ ๋ถํธ๋ฅผ ์ฐจ๋ก๋ก ๋ด์ boolean ๋ฐฐ์ด signs, ์ค์ ์ ์๋ค์ ํฉ ๊ตฌํ๊ธฐ
1) absoultes ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ๋ฐ๋ณตํ๊ธฐ
2) signs ๋ฐฐ์ด์ ๊ฐ์ด true ์ผ ๋ ๊ฒฐ๊ณผ ๋ณ์์ธ result์ absolutes[i]๋ฅผ ๋ํ๊ณ false์ผ ๋ result์ absoultes[i]๋ฅผ ๊ฐ์ํ๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public int solution(int[] absolutes, boolean[] signs) {
int result = 0;
for(int i=0;i<absolutes.length;i++)
{
if(signs[i] == true)
{
result += absolutes[i];
}
else if(signs[i] == false)
{
result -= absolutes[i];
}
}
return result;
}
}
17) ์๋ ์ซ์ ๋ํ๊ธฐ
0~9๊น์ง ์ซ์ ์ผ๋ถ๊ฐ ๋ค์ด์๋ ์ ์ ๋ฐฐ์ด์์ ์ฐพ์ ์ ์๋ 0๋ถํฐ 9๊น์ง์ ์ซ์๋ฅผ ๋ชจ๋ ์ฐพ์ ๋ํ ์๋ฅผ ๋ฐํ
1) 0~9๊น์ง์ ํฉ ๋ณ์์์ numbers ๋ฐฐ์ด ๊ฐ ๋นผ๊ธฐ
1
2
3
4
5
6
7
8
9
10
class Solution {
public int solution(int[] numbers) {
int answer = 45;
for(int i=0;i<numbers.length;i++)
{
answer -= numbers[i];
}
return answer;
}
}
18) ๋๋์ด ๋จ์ด์ง๋ ์ซ์ ๋ฐฐ์ด
๋ฐฐ์ด arr์ divisor๋ก ๋๋์ด ๋จ์ด์ง๋ ๊ฐ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ ๋ฐฐ์ด ๊ตฌํ๊ธฐ
1) arr ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ๊ธฐ
2) arr ๋ฐฐ์ด์ divisor๋ก ๋๋์์ ๋ ๋๋์ด ๋จ์ด์ง๋ ๊ฒฝ์ฐ ํ์ ์ธ๊ธฐ
3) ํ์ ํฌ๊ธฐ์ result ๋ฐฐ์ด ์์ฑ
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
29
30
31
32
import java.util.*;
class Solution {
public int[] solution(int[] arr, int divisor) {
int[] answer = {-1};
Arrays.sort(arr); // 5 7 9 10
int cnt= 0;
int idx = 0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]%divisor == 0)
{
cnt++;
}
}
if(cnt == 0)
{
return answer;
}
int[] result = new int[cnt];
for(int i=0;i<arr.length;i++)
{
if(arr[i]%divisor==0)
{
result[idx] = arr[i];
idx++;
}
}
return result;
}
}
19) ์ ์ผ ์์ ์ ์ ๊ฑฐํ๊ธฐ
์ ์๋ฅผ ์ ์ฅํ ๋ฐฐ์ด arr์์ ๊ฐ์ฅ ์์ ์๋ฅผ ์ ๊ฑฐํ ๋ฐฐ์ด ๊ตฌํ๊ธฐ
1) ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ 1์ด๋ฉด -1 ๋ฆฌํด
2) ๋ฐฐ์ด์์ ์ ์ผ ์์ ๊ฐ ๊ตฌํด์ ํด๋น ์ธ๋ฑ์ค ๊ฐ ์ ์ฅํ๊ธฐ
3) ๊ฐ์ฅ ์์ ์๋ฅผ ์ ๊ฑฐํ๋ฉด ํฌ๊ธฐ๊ฐ 1๋ง ๊ฐ์ํ๊ธฐ ๋๋ฌธ์ arr.length-1์ธ ๋ฐฐ์ด ์๋ก ๋ง๋ค๊ธฐ
4) idx๋ฅผ ์ ์ธํ๊ณ ๊ฒฐ๊ณผ๊ฐ ๊ตฌํ๊ธฐ
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
29
30
31
32
class Solution {
public int[] solution(int[] arr) {
int[] answer = {-1};
int min = Integer.MAX_VALUE;
int idx = 0;
if(arr.length == 1)
{
return answer;
}
for(int i=0;i<arr.length;i++)
{
if(arr[i]<min)
{
min = arr[i];
idx = i;
}
}
int[] result = new int[arr.length-1];
int resultIdx = 0;
for(int i=0;i<arr.length;i++)
{
if(i!= idx)
{
result[resultIdx] = arr[i];
resultIdx++;
}
}
return result;
}
}
20) ํธ๋ํฐ ๋ฒํธ ๊ฐ๋ฆฌ๊ธฐ
ํธ๋ํฐ ๋ฒํธ์์ ๋ท์๋ฆฌ 4์๋ฆฌ๋ฅผ ์ ์ธํ ๋๋จธ์ง ์ซ์ *๋ก ๋ฐ๊พธ๊ธฐ
1) ๋ฐ๋ณต๋ฌธ์ ์ด์ฉํด 4์๋ฆฌ๋ฅผ ์ ์ธํ๊ณ *๋ก ๋ณํ
2) substring์ ์ด์ฉํด ๋ค 4์๋ฆฌ๋ฅผ ์ถ์ถํ๊ธฐ. ex)
phone_number.length()-4
์ด๋ฉด ํด๋น ์ธ๋ฑ์ค๋ถํฐ ๋๊น์ง ์ถ์ถ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public String solution(String phone_number) {
String answer = "";
for(int i=0;i<phone_number.length()-4;i++)
{
answer += "*";
}
// ๋ง์ง๋ง ๋ค์๋ฆฌ
String phone = phone_number.substring(phone_number.length()-4);
answer+=phone;
return answer;
}
}