Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/bruteForceSearch/pg_42839.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package bruteForceSearch;

/*
* 소수 찾기, level2 : https://school.programmers.co.kr/learn/courses/30/lessons/42839
* 1. 조합으로 숫자의 경우의 수를 찾고
* 2. for문을 돌면서 나눠지는 숫자가 있는지 확인한다. (없으면 소수)
*
* 훔냐ㅑ 답안지 슥삭 했습니다.....
* 슥삭한 블로그 : https://dding9code.tistory.com/18
* 1. 소수를 판단하는 함수 + DFS로 숫자를 조합하는 함수를 구현한다.
* */

import java.util.ArrayList;

public class pg_42839 {

// 숫자 조합을 저장할 ArrayList
static ArrayList<Integer> arr = new ArrayList<>();
// 같은 숫자 중복을 확인하기 위한 배열
static boolean[] check = new boolean[7];

public int solution(String numbers) {
int answer = 0;

for (int i = 0; i < numbers.length(); i++) {
dfs(numbers, "", i + 1);
}

for (int i = 0; i < arr.size(); i++) {
if (prime(arr.get(i))) {
answer++;
}
}

return answer;
}

private void dfs(String numbers, String s, int m) {
if (s.length() == m) {
int num = Integer.parseInt(s);
if (!arr.contains(num)) { // 이미 만들어진 숫자 거르기
arr.add(num);
}
}
for (int i = 0; i < numbers.length(); i++) {
if (!check[i]) {
check[i] = true;
s += numbers.charAt(i);
dfs(numbers, s, m);
check[i] = false;
s = s.substring(0, s.length() - 1);
}
}
}

private boolean prime(int n) {
if (n < 2) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
25 changes: 25 additions & 0 deletions src/bruteForceSearch/pg_42842.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package bruteForceSearch;

/*
* 완전탐색 level2
* 카펫 :https://school.programmers.co.kr/learn/courses/30/lessons/42842
* 노란색의 가로 세로를 x, y | 갈색의 가로 세로를 x+2, y+2 로 두고 넓이를 이용한 방정식을 통해 해결
* */

public class pg_42842 {

public int[] solution(int brown, int yellow) {
int x = 0;
// y = brownSum - x;
int brownSum = (brown - 4) / 2;

for (int i = 0; i < 2500; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전 0인 경우는 존재하지 않는다고 생각해서( 이유 : yellow 가 1 이상이면 그럴 일이 없습니다) 3부터 돌아줬읍니당

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맞아요ㅋㅎㅋㅎ 사실 2500까지도 안갔어도 됐을 것 같습니다ㅎㅎㅎ 반성반성

if (i * (brownSum - i) == yellow) {
x = i;
break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 걍 i 를 밖에서 선언하고 break 해서 그대로 써도 됐을 듯 합니다!! 하지만 이정도는 취향일지도ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

지수님 처럼! 맞아여 for문안에서 return 할 생각을 못함ㅋㅋㅋ

}
}
int[] answer = {brownSum + 2 - x, x + 2};
return answer;
}
}