Skip to content

Commit cefe121

Browse files
committed
feat : 프로그래머스 완전탐색
1 parent e8ebed6 commit cefe121

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sgyj.programmers.yeji.cs;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class Solution42839 {
7+
private static boolean[] visited;
8+
private static Set<Integer> answer = new HashSet<>();
9+
10+
public static int solution(String numbers) {
11+
String[] numberStr = numbers.split("");
12+
int n = numberStr.length;
13+
visited = new boolean[n];
14+
for(int i=0; i<n; i++){
15+
visited[i] = true;
16+
checkPrime(numberStr,i,n,numberStr[i],0);
17+
}
18+
return answer.size();
19+
}
20+
21+
public static void checkPrime(String[] numberStr, int i, int n,String target, int count){
22+
if(i==n && count != n) i=0;
23+
if(i==n || count == n) return;
24+
if(!visited[i]){
25+
target += numberStr[i];
26+
}
27+
if(isPrime(Integer.valueOf(target))){
28+
answer.add(Integer.valueOf(target));
29+
}
30+
visited[i] = true;
31+
checkPrime(numberStr,i+1,n,target,count+1);
32+
visited[i] = false;
33+
}
34+
35+
public static boolean isPrime(int target){
36+
boolean isPrime = true;
37+
if(target == 0 || target == 1) return false;
38+
for(int p=2; p<target; p++){
39+
if(target % p == 0) {
40+
isPrime = false;
41+
}
42+
}
43+
return isPrime;
44+
}
45+
46+
public static void main ( String[] args ) {
47+
String numbers = "17";
48+
System.out.println(solution(numbers));
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sgyj.programmers.yeji.cs;
2+
3+
public class Solution42842 {
4+
public static int[] solution(int brown, int yellow) {
5+
int[] answer = new int[2];
6+
int karo = yellow;
7+
int sero = 1;
8+
9+
while(sero <= karo){
10+
int comapreBrown = (karo * 2) + (2 * sero) + 4;
11+
if(comapreBrown == brown){
12+
answer[0] = karo+2;
13+
answer[1] = sero+2;
14+
return answer;
15+
}
16+
sero++;
17+
karo = (yellow / sero) + (yellow % sero);
18+
}
19+
20+
return answer;
21+
}
22+
23+
public static void main ( String[] args ) {
24+
int brown = 10;
25+
int yellow = 2;
26+
for(int s : solution(brown,yellow)){
27+
System.out.print( s + " " );
28+
}
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sgyj.programmers.yeji.cs;
2+
3+
public class Solution87946 {
4+
private static boolean[] check;
5+
private static int answer = -1;
6+
7+
public static int solution(int k, int[][] dungeons) {
8+
check = new boolean[dungeons.length];
9+
10+
dfs(k,dungeons,0);
11+
12+
return answer;
13+
}
14+
15+
public static void dfs(int tired, int[][] dungeons, int cnt){
16+
for(int i=0; i<dungeons.length; i++){
17+
if(!check[i] && dungeons[i][0]<=tired){
18+
check[i] = true;
19+
dfs(tired-dungeons[i][1],dungeons,cnt+1);
20+
check[i] = false;
21+
}
22+
}
23+
answer = Math.max(answer,cnt);
24+
}
25+
public static void main ( String[] args ) {
26+
int k = 80;
27+
int[][] dungeons = {
28+
// [[80,20],[50,40],[30,10]]
29+
{80,20},{50,40},{30,10}
30+
};
31+
32+
System.out.println( solution(k,dungeons) );
33+
}
34+
}

0 commit comments

Comments
 (0)