Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

모든 경우의 수에 대한 탐색 #10

Open
JunilHwang opened this issue May 3, 2020 · 3 comments
Open

모든 경우의 수에 대한 탐색 #10

JunilHwang opened this issue May 3, 2020 · 3 comments

Comments

@JunilHwang
Copy link
Member

JunilHwang commented May 3, 2020

모든 경우의 수 탐색에 대한 방법은 알고리즘에서도 필요하고, 여러가지 문제해결 과정에서 꼭 필요합니다. 그래서 반드시 알아둘 필요가 있습니다.

javascript

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // 1 ~ 10으로 조합 가능한 모든 경우의 수를 출력하기
const len = arr.length
const f = stack => {
  if (stack.length === len) {
    console.log(stack)
    return
  }
  arr.forEach(v => {
    if (stack.indexOf(v) === -1) f([ ...stack, v])
  })
}
f([])
@JunilHwang
Copy link
Member Author

JunilHwang commented May 3, 2020

python

arr = [1, 2, 3] # 1 ~ 10으로 조합 가능한 모든 경우의 수를 출력하기
last = len(arr)

def f (stack):
    if len(stack) == last:
        print(stack)
        return

    for v in arr:
        if not v in stack:
            f(stack[:] + [v])

f([]) # 실행

@JunilHwang
Copy link
Member Author

java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class all {

  public static void f (List<Integer> stack, List<Integer> list) {
    if (stack.size() == list.size()) {
      System.out.println(stack);
      return;
    }
    for (int v : list) {
      if (!stack.contains(v)) {
        List<Integer> temp = stack.stream().collect(Collectors.toList());
        temp.add(v);
        f(temp, list);
      }
    }
  }

  public static void solution (int[] arr) {
    List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
    List<Integer> stack = new ArrayList();
    f(stack, list);
  }

  public static void main(String[] args) {
    int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    solution(arr);
  }
}

@JunilHwang
Copy link
Member Author

JunilHwang commented May 3, 2020

모든 경우의 수

@eyabc eyabc added the 필수 label May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants