Question: How many combinations can a maximum of 16 of the five ABCDE balls be extracted repeatedly?
For example, there are five combinations of a B c d e which can be composed of one ball, A combination of 5 + 4 + 3 + 2 + 1 = 15 (repeated arrangement of BA and AB is considered as one)
Aa AB ac ad AE
Bb bc bd be
CC CD CE
Dd de
EE
The combination of three balls is (5 + 4 + 3 + 2 + 1) + (4 + 3 + 2 + 1) + (3 + 2 + 1) + (2 + 1) + 1 = 35
Aaa aab aac aad AAE
Abb abc Abd Abe
Acc acd ace
Add Ade
Aee
Bbb bbc bbd bbe
BCC BCD BCE
BDD BDE
Bee
CCC CCD CCE
CDD CDE
CEE
Ddd DDE
Dee
Eee
CodeAs follows:
/*
* It is mainly implemented by recursion. The main idea is to divide a string into two segments for processing. First, extract the first string and use the following characters to piece it together.
*/
Import java. util. collections;
Public class zuhe {
Private Static string STR = "ABCDE"; // string
Private Static int n = 3; // number of options
Private Static int COUNT = 0; // number of combinations
Public static void main (string [] ARGs ){
New zuhe ();
}
Zuhe (){
Wrote input = new partition (system. In );
System. Out. println ("Enter the number to be selected (less than" + Str. Length () + )");
N = integer. parseint (input. nextline ());
Find ("", 0 );
System. Out. println ("Total" + Count + "Combination ");
}
/*
* The first parameter indicates the first character, and the second parameter indicates the position of the Start vertex.
*/
Public static void find (string S, int I ){
// Save the last string
String temp = s;
// Determine whether the request meets the requirements
If (S. Length () = N ){
Count ++;
System. Out. Print (S + "");
If (count % 10 = 0)
System. Out. println ();
Return;
}
// Starts from the search point to the wind condition
For (int K = I; k <Str. Length (); k ++ ){
S = temp;
S + = Str. charat (k );
Find (S, k );
}
}
}
The output result is as follows:
enter the number to be selected (less than 5)
3
aaa aab aac aad aae abb abc Abd Abe ACC
ACD ace add Ade aee bbb bbc bbd bbe BCC
bcd bce bdd bde bee CCC CCD cce cdd cde
cee ddd dde Dee EEE has 35 combinations