😁 무식하게 N개의 원소중 M개를 고르는 모든 조합 방법 (재귀함수)
public class RecourSive {
public static void main(String[] args) {
Vector<Integer> dataSet = new Vector<>();
choiceNumber(6,dataSet,3);
}
public static void choiceNumber(int n,Vector<Integer> dataSet,int choice){
if(choice ==0) {
dataSet.forEach(System.out::print);
System.out.println();
};
int smallest = dataSet.isEmpty() ? 0 : dataSet.get(dataSet.size()-1)+1;
for(int next = smallest; next < n; ++next){
dataSet.add(next);
choiceNumber(n,dataSet,choice-1);
dataSet.remove(dataSet.size() - 1);
}
};
}
출력 결과
012
013
014
015
023
024
025
034
035
045
123
124
125
134
135
145
234
235
245
345
Process finished with exit code 0