import java.util.Arrays;
基于//二进制算法的全数组
//count1:170187
//count2:291656
公共类测试{
publicstaticvoidmain (字符串薄片月饼args )。
long start=system.current time millis (;
count2(;
longend=system.current time millis (;
}
私有静态语音计数2 (
int薄月饼num=new int薄月饼{ 1,2,3,4,5,6,7,8,9 };
for(intI=1; I
stringstr=integer.tostring(I,9 );
int sz=str.length (;
for(intj=0; j9-sz; j ) {
}
char薄月饼temp=str.toCharArray (;
Arrays.sort(temp );
字符串GL=新字符串(temp );
if (! GL.equals(012345678 ) ) ) }
继续;
}
字符串结果=' ';
for(intm=0; 米
result=num [ integer.parseint (str.charat (m ) ') ];
}
}
}
公共静态语音计数1 (
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];
wile(temp[0]9) {
temp[temp.length-1];
for(intI=temp.length-1; i0; I---- ) {
if(temp[I]==9) {
temp[i]=0;
temp[i-1];
}
}
int薄月饼tt=temp.clone (;
Arrays.sort(TT;
if (! Arrays.equals(TT,ss ) }{
继续;
}
字符串结果=' ';
for(intI=0; I
result=num[temp[i]];
}
}
}
}
一、利用二进制状态法求解序列组合。 该方法简单易懂,但执行效率高,可以使用小数据数组的组合
2 .用递归思想求数组和组合,代码量很大
打包实践;
import java.util.ArrayList;
import java.util.List; 公共类测试1 {/* *
* @param args*/
publicstaticvoidmain (字符串薄片月饼args ) {//todo auto-generated method stub
Object薄月饼tmp={1、2、3、4、5、6}; //ArrayList RS=随机(tmp );
//system.out.print(I'=';
for(intj=0; Jj
{
}
}
求出mdgb组的任意组合
staticarraylistrandomc (对象薄片月饼source ) )。
{
{
result.add(source;
}else{
Object薄月饼p source=new object [ source.length-1 ]; for(intI=0; I
{
psource[i]=source[i];
}
result=随机源(p source ); int len=result.size (; //fn组合的长度
result.add ((新对象薄片月饼) {source[source.length-1]} ); for(intI=0; I
{
Object薄月饼tmp=newobject[result.get(I ).length 1] for(intj=0; Jj
{
tmp[j]=result.get(i;
}
tmp [ tmp.length-1 ]=source [ source.length-1 ];
result.add(tmp;
}
}返回结果;
} staticarraylistcmn (对象薄片月饼source,intn ) ) ) ) ) ) ) )。
{
{for(intI=0; I
{
result.add (新对象薄月饼({source[i]} );
}
}elseif(source.length==n ) ) ) ) ) ) )。
{
result.add(source;
}else{
Object薄月饼p source=new object [ source.length-1 ]; for(intI=0; I
{
psource[i]=source[i];
}
结果=cmn (p source,n );
{
Object薄月饼rs=newObject[n]; for(intj=0; Jj
{
RS[j]=tmp.get[I][j];
}
RS [ n-1 ]=source [ source.length-1 ];
result.add(RS;
}
}返回结果;
}
}
3 .利用动态规划的思想求数组和组合
打包广告; //求出强力组合数
publicclassmainapp { publicstaticvoidmain (字符串薄片月饼args ) int薄片月饼num=new int薄片月饼) 1、2、3、4、4、5 };
String str=' '; 3mdgb组合求出mdgb//count(0,str,num,3 )求出1-nmdgb的组合mdgb
计数1 (0,str,num );
}私有月饼计数1 (inti,String str,int薄月饼num ) if(I==num.Length ) )。
}
计数1 (i1,str,num );
count1(I1,str num[i] ',',num );
}隐私声明(inti,String str,Int薄月饼num,intn ) (if(n==0) ) ) ) ) ) )。
(if ) I==num.Length ) {return;
}
count(I1,str num[i] ',',num,n-1 );
计数(i1,str,num,n );
}
}
以下求出排列
打包广告; //求排列,求各种排列和组合排列
importjava.util.Arrays; importjava.util.Scanner; public class demo 19 { privatestaticbooleanf薄月饼; publicstaticvoidmain (字符串薄片月饼args )。
sannersc=newscanner(system.in; int sz=sc.nextInt (; for(intI=0; I
f=new boolean[sum];
Arrays.fill(f,true ); int薄月饼num=new int[sum]; for(intj=0; Jj
num[j]=j 1;
(}int nn=sc.nextInt );
String str=' ';
count(num,str,nn );
}
(/) **
*@paramnum表示数组
*@paramstr是一个排列的字符串
* @如果剩下需要*@paramnn序列的mdgb,需要全部序列,则nn为序列长度*/
私有月饼计数(int薄片月饼num,String str,intnn ) (if(nn==0) )。
}for(intI=0; I
}
f[i]=false;
count(num,str num[i],nn-1 );
f[i]=true;
}
}
}