I. Using the binary state method to find the arrangement and combination is easier to understand, but the operation efficiency is not high. Small Data arrangement and combination can be used.
Copy codeThe Code is as follows:
Import java. util. Arrays;
// Use the binary algorithm for full sorting
// Count1: 170187
// Count2: 291656
Public class test {
Public static void main (String [] args ){
Long start = System. currentTimeMillis ();
Count2 ();
Long end = System. currentTimeMillis ();
System. out. println (end-start );
}
Private static void count2 (){
Int [] num = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9 };
For (int I = 1; I <Math. pow (9, 9); I ++ ){
String str = Integer. toString (I, 9 );
Int sz = str. length ();
For (int j = 0; j <9-sz; j ++ ){
Str = "0" + str;
}
Char [] temp = str. toCharArray ();
Arrays. sort (temp );
String gl = new String (temp );
If (! Gl. equals ("012345678 ")){
Continue;
}
String result = "";
For (int m = 0; m <str. length (); m ++ ){
Result + = num [Integer. parseInt (str. charAt (m) + "")];
}
System. out. println (result );
}
}
Public static void count1 (){
Int [] num = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Int [] ss = new int [] {0, 1, 2, 3, 4, 5, 6, 7, 8 };
Int [] temp = new int [9];
While (temp [0] <9 ){
Temp [temp. length-1] ++;
For (int I = temp. length-1; I> 0; I --){
If (temp [I] = 9 ){
Temp [I] = 0;
Temp [I-1] ++;
}
}
Int [] tt = temp. clone ();
Arrays. sort (tt );
If (! Arrays. equals (tt, ss )){
Continue;
}
String result = "";
For (int I = 0; I <num. length; I ++ ){
Result + = num [temp [I];
}
System. out. println (result );
}
}
}
Ii. Use recursive thinking to find the arrangement and combination, with a large amount of code
Copy codeThe Code is as follows:
Package practice;
Import java. util. ArrayList;
Import java. util. List;
Public class Test1 {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Object [] tmp = {1, 2, 4, 5, 6 };
// ArrayList <Object []> rs = RandomC (tmp );
ArrayList <Object []> rs = CEN (tmp, 3 );
For (int I = 0; I <rs. size (); I ++)
{
// System. out. print (I + "= ");
For (int j = 0; j <rs. get (I). length; j ++)
{
System. out. print (rs. get (I) [j] + ",");
}
System. out. println ();
}
}
// Obtain any combination of an array
Static ArrayList <Object []> RandomC (Object [] source)
{
ArrayList <Object []> result = new ArrayList <Object []> ();
If (source. length = 1)
{
Result. add (source );
}
Else
{
Object [] psource = new Object [source. length-1];
For (int I = 0; I <psource. length; I ++)
{
Psource [I] = source [I];
}
Result = RandomC (psource );
Int len = result. size (); // The length of the fn combination
Result. add (new Object [] {source [source. length-1]});
For (int I = 0; I <len; I ++)
{
Object [] tmp = new Object [result. get (I). length + 1];
For (int j = 0; j <tmp. length-1; j ++)
{
Tmp [j] = result. get (I) [j];
}
Tmp [tmp. length-1] = source [source. length-1];
Result. add (tmp );
}
}
Return result;
}
Static ArrayList <Object []> CEN (Object [] source, int n)
{
ArrayList <Object []> result = new ArrayList <Object []> ();
If (n = 1)
{
For (int I = 0; I <source. length; I ++)
{
Result. add (new Object [] {source [I]});
}
}
Else if (source. length = n)
{
Result. add (source );
}
Else
{
Object [] psource = new Object [source. length-1];
For (int I = 0; I <psource. length; I ++)
{
Psource [I] = source [I];
}
Result = CEN (psource, n );
ArrayList <Object []> tmp = CEN (psource, n-1 );
For (int I = 0; I <tmp. size (); I ++)
{
Object [] rs = new Object [n];
For (int j = 0; j <n-1; j ++)
{
Rs [j] = tmp. get (I) [j];
}
Rs [n-1] = source [source. length-1];
Result. add (rs );
}
}
Return result;
}
}
3. Seek arrangement and combination using the idea of Dynamic Planning
Copy codeThe Code is as follows:
Package Acm;
// Powerful combination quantity
Public class MainApp {
Public static void main (String [] args ){
Int [] num = new int [] {1, 2, 3, 4, 5 };
String str = "";
// Calculate the number of Three Combinations
// Count (0, str, num, 3 );
// Calculate the number of combinations of 1-N numbers
Count1 (0, str, num );
}
Private static void count1 (int I, String str, int [] num ){
If (I = num. length ){
System. out. println (str );
Return;
}
Count1 (I + 1, str, num );
Count1 (I + 1, str + num [I] + ",", num );
}
Private static void count (int I, String str, int [] num, int n ){
If (n = 0 ){
System. out. println (str );
Return;
}
If (I = num. length ){
Return;
}
Count (I + 1, str + num [I] + ",", num, n-1 );
Count (I + 1, str, num, n );
}
}
The following is an arrangement.
Copy codeThe Code is as follows:
Package Acm;
// Sort by arrangement. sort by arrangement or combination.
Import java. util. Arrays;
Import java. util. writable; public class Demo19 {
Private static boolean f [];
Public static void main (String [] args ){
Pipeline SC = new pipeline (System. in );
Int sz = SC. nextInt ();
For (int I = 0; I <sz; I ++ ){
Int sum = SC. nextInt ();
F = new boolean [sum];
Arrays. fill (f, true );
Int [] num = new int [sum];
For (int j = 0; j <sum; j ++ ){
Num [j] = j + 1;
}
Int nn = SC. nextInt ();
String str = "";
Count (num, str, nn );
}
}
/**
*
* @ Param num indicates the array to be arranged
* @ Param str to sort strings
* @ Param nn: the number of remaining parts to be arranged. If you want to arrange them in full, nn indicates the length of the array.
*/
Private static void count (int [] num, String str, int nn ){
If (nn = 0 ){
System. out. println (str );
Return;
}
For (int I = 0; I <num. length; I ++ ){
If (! F [I]) {
Continue;
}
F [I] = false;
Count (num, str + num [I], nn-1 );
F [I] = true;
}
}
}