Algorithm Description:
Fully arranged N-base algorithm --
From 1 to n, the output is all arranged, N in total! .
Analysis: N-base method. Set an array of N units. array a is used to store the subscript of the array to be fully arranged. Add one to the first unit. Each time you add an array, check whether there are duplicates in each array unit. If yes, convert it back to the add-on operation. If no, array A has no element with the value of N) it indicates that an arrangement scheme is obtained.
For example, you can find 3 in the full arrangement of 1-3! Entries
Set the initial status of the array to 0 0 0. perform the following steps to calculate the full arrangement:
0 0 0 + 1
1 0 0 + 1
2 0 0 + 1
3 0 0 full 3 into 1->
0 1 0 + 1
1 1 0 + 1
2 1 0 + 1
3 1 0 full 3 into 1->
0 2 0 + 1
1 2 0 + 1
2 2 0 + 1
3 2 0 full 3 into 1->
0 3 0 full 3 into 1->
0 0 1 + 1
1 0 1 + 1
2 0 1 + 1
3 0 1 full 3 into 1->
0 1 1 + 1
1 1 1 + 1
2 1 1 + 1
3 1 1 full 3 into 1->
0 2 1 + 1
1 2 1 + 1
2 2 1 + 1
3 2 1 full 3 into 1->
0 3 1 full 3 into 1->
0 0 2 + 1
1 0 2 + 1
2 0 2 + 1
3 0 2 full 3 into 1->
0 1 2 + 1
1 1 2 + 1
2 1 2 + 1
3 1 2 full 3 into 1->
0 2 2 + 1
1 2 2 + 1
2 2 2 + 1
3 2 2 full 3 into 1->
0 3 2 full 3 into 1->
0 0 3 full 3 into 1-> (the highest bit is hexadecimal N (here is 3) is the condition for loop Exit)
0 0 0
The six subscript sequences indicated by the red background color are qualified, and each subscript sequence corresponds to an arrangement.
The implementation is as follows:
====================================
# Include <iostream>
Using namespace STD;
// If the subscript sequence contains hexadecimal N or the sequence contains repeated numbers, the return value is true.
Bool checksame (int * a, int N)
{
For (INT I = 0; I <n-1; I ++)
{
If (A [I] = N)
Return true;
For (Int J = I + 1; j <n; j ++)
If (A [I] = A [J])
Return true;
}
Return false;
}
Int main ()
{
Int N;
Char c = 'y ';
While (C = 'y ')
{
Cout <"input N :";
Cin> N;
Int * A = new int [N]; // array a is used to store subscript
// Array B is used to store the actual numbers to be arranged, which can be manually defined.
Int * B = new int [N];
For (INT I = 0; I <n; I ++)
{
A [I] = 0;
B [I] = I + 1;
}
Int COUNT = 0; // stores the total number of sorting items.
While (A [n-1]! = N) // The highest bit is the hexadecimal N, which is the condition for loop exit.
{
// Print the current full arrangement first
If (! Checksame (A, n ))
{
For (INT I = 0; I <n; I ++)
{
Cout <B [A [I] <"";
If (I = N-1)
Count ++;
}
Cout <Endl;
}
// Complete carry
Bool increase = false;
For (INT I = 0; I <n-1; I ++)
{
// If there is N, carry is completed
If (A [I] = N)
{
A [I] = 0;
A [I + 1] ++;
Increase = true; // bitwise
Break;
}
}
// If no bitwise is entered, add the bitwise to 1
If (! Increase)
A [0] ++;
}
Cout <"total number is:" <count <Endl;
Delete [];
Delete [] B;
A = B = NULL;
Cout <"continue? (Y/N ):";
Cin> C;
}
Return 0;
}
In addition, the non-recursive combination algorithm implemented by binary method can be found in: http://blog.csdn.net/Deutschester/archive/2010/06/05/5649328.aspx.