Assume that the number of values from 1 to 8 is 4.
1) Minimum values (1, 2, 3, 4)
2) assume that the current (A, B, C, D), if we can determine the combination of the Weight Value and the combination that is just greater than it, it is not difficult to find all the combinations by weight and order
Data Structure
Two structures are required: selects and remains. Selects is the selected child set and remains is the number to be considered.
They are lists and need to be sorted. First, the elements are placed as follows:
Algorithm
1) Use the smallest element in remains to find the element in selects that is better than it can.
2) If the two elements are found, exchange them.
3) if it cannot be found, delete the element from remains and take the next element to continue searching (that is, to 1 ))
------------ What can be found
Selects
Remains
Selects
Remains
------------ Cannot be found
Selects
Remains
Selects
Remains
(1. You don't have to worry about it. Adding selects will inevitably reduce the weight)
========================= SpecificProgramBelow ============================
Package com. PNP. findnextnumber;
Import java. util. arraylist;
Import java. util. collections;
Public class nextweighsum {
Arraylist <integer> remains = new arraylist <integer> ();
Arraylist <integer> selects = new arraylist <integer> ();
Int total_count = 10;
Int select_count = 4;
Void Init (){
For (INT I = 0; I <total_count; I ++)
Remains. Add (I + 1 );
Collections. Sort (remains );
For (INT I = 0; I <select_count; I ++)
Selects. Add (remains. Remove (0 ));
}
/*
* Selects give the subset, need to return the next subset which the weight sum is just larger than current subset
*/
Boolean selectnext (){
While (remains. Size ()> 0 ){
Int cur = remains. Get (0 );
Int Pos = collections. binarysearch (selects, cur );
If (Pos <0) // binarysearch (-(insertion point)-1)
Pos = (-Pos)-1;
Else {
System. Err. Print ("not allow equal elements ");
System. Exit (-1); // not allow equal elements
}
If (Pos = 0) {// That means current element is less that any in selects, we won't need to consider this ELEM
Remains. Remove (0 );
Continue;
}
Else {
Int insert_pos = pos-1;
Remains. Set (0, selects. Get (insert_pos ));
Selects. Set (insert_pos, cur );
System. Out. Print ("an Select ---");
Print (selects );
Return true;
}
}
Return false;
}
Void selectall (){
While (selectnext ())
;
}
Static void print (arraylist <integer> List ){
Int sum = 0;
For (INT I = 0; I <list. Size (); I ++ ){
Sum + = list. Get (I );
System. Out. Print ("" + list. Get (I ));
}
System. Out. println ("sum:" + sum );
}
Public static void main (string [] ARGs ){
Nextweighsum M = new nextweighsum ();
M. INIT ();
M. selectall ();
}
}
This algorithm is intended to be used in the question of goods Lang. The question of goods Lang is the set of options and the smallest edge. It is used to test whether it constitutes a path. If it constitutes a path, the problem is solved. Otherwise, it will test the next one.