First, using the binary state method to find the permutation combination, this method is easy to understand, but the operation efficiency is not high, small data permutation combination can be used
Copy Code code as follows:
import java.util.Arrays;
Using binary algorithm to arrange all
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);
}
}
}
Two. With the idea of recursion to find permutations and combinations, the amount of code is relatively large
Copy Code code 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,3,4,5,6};
Arraylist<object[]> RS=RANDOMC (TMP);
Arraylist<object[]> rs=cmn (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 ();
}
}
//find 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++)
{ br> Psource[i]=source[i];
}
Result=randomc (psource);
int len=result.size ();//fn the length of the 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[]> cmn (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=cmn (psource,n);
Arraylist<object[]> tmp=cmn (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;
}
}
Three. Use the idea of dynamic programming to find the arrangement and combination
Copy Code code as follows:
package Acm;
//strong number of combinations
public class Mainapp {
public static void Main (string[] args) {
int[] Num=new int[]{1,2,3,4,5};
String str= "";
//Find the number of combinations of 3 numbers
//Count (0,str,num,3);
//1-n number of combinations
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);
}
}
Here is the alignment
Copy Code code as follows:
Package Acm;
//alignment, after various permutations or combinations
import java.util.Arrays;
import java.util.Scanner; public class Demo19 {
private static Boolean f[];
public static void Main (string[] args) {
Scanner sc=new Scanner (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 arrange
* @param str with the arranged string
* @param the number of remaining permutations to NN, if all is required, nn is an array length
*/
private static void count (int[] num, String str, int nn) {
if (nn==0) {
System.out.println (str);
RET Urn
}
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;
}
}
}