Combinatorial algorithms (similar to C (5,2))

Source: Internet
Author: User

/**
* Get Combinations (C (superscript, subscript))
*@paramTotalCountNormal data node group count except primary data node group
*@paramSelectCountSecondary data node count
*@returnThe possibility of combinations
* e.g. the possibility of select 2 members from 5 members
* The possibility: {[5,4], [5,3], [5,2], [5,1], [4,3], [4,2], [4,1], [3,2], [3,1], [2,1]}
*/
Private list<deque<integer>>Getcombination (int TotalCount, int selectcount) {
The combination of secondary count from group Count which expect primary group.
int comb[] =New int[selectcount+1];
comb[0] = SelectCount;

List<deque<integer>> resultindexlist =New Arraylist<> ();
Combination (comb, TotalCount, SelectCount, resultindexlist);

Logger.info ("Secondary group Index combination:{}", resultindexlist);
Return resultindexlist;
}

/**
* Get Combinations (C (superscript, subscript))
* e.g. C (5, 2), the resultlist: {[5,4], [5,3], [5,2], [5,1], [4,3], [4,2], [4,1], [3,2], [3,1], [2,1]}
*@paramCombLast result of combination
*@paramSuperscriptMax of the number
*@paramSubscriptSelect numbers count
*@paramResultlistResult after combination
*/
private voidCombination (int comb[], int superscript, int subscript, list<deque<integer>> resultlist) {
for (int i = superscript; I >= subscript; i--) {
Select Current HEAD element
Comb[subscript] = i;

if (Subscript >1) {
Enter next smaller combination
Combination (comb, I-1, subscript-1;
} else {
Deque<integer> selectindexqueue = Span style= "COLOR: #cc7832" >new arraydeque<> () //save combination
Span style= "COLOR: #cc7832" >for (int j=comb[0]; J>0 Selectindexqueue.add (comb[j]) ;
}
Resultlist.add (selectindexqueue) ;
}
}
return;
}
/span>

Combinatorial algorithm (similar to C (5,2))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.