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 ();
}
}
To 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++)
{
Psource[i]=source[i];
}
RESULT=RANDOMC (Psource);
int len=result.size ();//fn length of 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);
To find the number of combinations of 1-n
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;
To arrange, for a variety of 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 represents the array to arrange
* @param str with a string of good arrangement
* @param the number of remaining NN needs to be arranged, and 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);
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;
}
}
}