본문 바로가기

Study/Java

Combination 구하기 (Number of Cases)

지금도 진행중이지만, 발생할수 있는 경우의 수를 계산할일이 생겼는데, 

이제는 산수도 헷갈리는 문제가 발생하였습니다. 


경우의 수를 쉽게 구하는 방법이 있습니다.(예를들면 M이라는 갯수가 있을때 N개씩 뽑을대 발생할수 있는 McN) 경우의 수,, 표현식이 맞는지 잘모르겠지만..


예전에 울프럼알파? 수학용 검색엔진이 있다고 언듯들은적 있는데, 바로 계산해주네요 




그리고 만약 자바에서 할경우에는, 하마타면 로직을 만들뻔 했지만 combinatoricslib 라이브러리가 존재,

https://code.google.com/p/combinatoricslib


            <dependency>

<groupId>com.googlecode.combinatoricslib</groupId>

<artifactId>combinatoricslib</artifactId>

<version>2.0</version>

</dependency>



홈페이지에는 참고할만한 내용이 없을 정도로 라이브러리 자체가 심플하게 구성되어 있네요 \

 // Create the initial vector of (apple, orange)
   
ICombinatoricsVector<String> initialVector = Factory.createVector(
     
new String[] { "apple", "orange" } );

   
// Create a multi-combination generator to generate 3-combinations of
   
// the initial vector
   
Generator<String> gen = Factory.createMultiCombinationGenerator(initialVector, 3);

   
// Print all possible combinations
   
for (ICombinatoricsVector<String> combination : gen) {
     
System.out.println(combination);
   
}