java算法實現排列組合的方法介紹
一.利用二進制狀態法求排列組合,此種方法比較容易懂,但是運行效率不高,小數據排列組合可以使用代碼如下:
import ys;
//利用二進制算法進行全排列
//count1:170187
//count2:291656
public class test {
public static void main(String[] args) {
long start=entTimeMillis();
count2();
long end=entTimeMillis();
tln(end-start);
}
private static void count2(){
int[] num=new int []{1,2,3,4,5,6,7,8,9};
for(int i=1;i<(9, 9);i++){
String str=ring(i,9);
int sz=th();
for(int j=0;j<9-sz;j++){
str="0"+str;
}
char[] temp=arArray();
(temp);
String gl=new String(temp);
if(!ls("012345678")){
continue;
}
String result="";
for(int m=0;m<th();m++){
result+=num[eInt(At(m)+"")];
}
tln(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[th-1]++;
for(int i=th-1;i>0;i--){
if(temp[i]==9){
temp[i]=0;
temp[i-1]++;
}
}
int []tt=e();
(tt);
if(!ls(tt,ss)){
continue;
}
String result="";
for(int i=0;i<th;i++){
result+=num[temp[i]];
}
tln(result);
}
}
}
二.用遞歸的思想來求排列跟組合,代碼量比較大代碼如下:
package practice;
import yList;
import ;
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
rs=RandomC(tmp);
ArrayList
rs=cmn(tmp,3);
for(int i=0;i<();i++)
{
// t(i+"=");
for(int j=0;j<(i)th;j++)
{
t((i)[j]+",");
}
tln();
}
}
// 求一個數組的任意組合
static ArrayList
RandomC(Object[] source)
{
ArrayList
result=new ArrayList
();
if(th==1)
{
(source);
}
else
{
Object[] psource=new Object[th-1];
for(int i=0;i<th;i++)
{
psource[i]=source[i];
}
result=RandomC(psource);
int len=();//fn組合的長度
((new Object[]{source[th-1]}));
for(int i=0;i<len;i++)
{
Object[] tmp=new Object[(i)th+1];
for(int j=0;j<th-1;j++)
{
tmp[j]=(i)[j];
}
tmp[th-1]=source[th-1];
(tmp);
}
}
return result;
}
static ArrayList
cmn(Object[] source,int n)
{
ArrayList
result=new ArrayList
();
if(n==1)
{
for(int i=0;i<th;i++)
{
(new Object[]{source[i]});
}
}
else if(th==n)
{
(source);
}
else
{
Object[] psource=new Object[th-1];
for(int i=0;i<th;i++)
{
psource[i]=source[i];
}
result=cmn(psource,n);
ArrayList
tmp=cmn(psource,n-1);
for(int i=0;i<();i++)
{
Object[] rs=new Object[n];
for(int j=0;j<n-1;j++)
{
rs[j]=(i)[j];
}
rs[n-1]=source[th-1];
(rs);
}
}
return result;
}
}
三.利用動態規劃的思想求排列和組合 代碼如下:
package Acm;
//強大的求組合數
public class MainApp {
public static void main(String[] args) {
int[] num=new int[]{1,2,3,4,5};
String str="";
//求3個數的組合個數
// count(0,str,num,3);
// 求1-n個數的組合個數
count1(0,str,num);
}
private static void count1(int i, String str, int[] num) {
if(i==th){
tln(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){
tln(str);
return;
}
if(i==th){
return;
}
count(i+1,str+num[i]+",",num,n-1);
count(i+1,str,num,n);
}
}
下面是求排列代碼如下:
package Acm;
//求排列,求各種排列或組合後排列
import ys;
import ner;
public class Demo19 {
private static boolean f[];
public static void main(String[] args) {
Scanner sc=new Scanner();
int sz=Int();
for(int i=0;i<sz;i++){
int sum=Int();
f=new boolean[sum];
(f, true);
int[] num=new int[sum];
for(int j=0;j<sum;j++){
num[j]=j+1;
}
int nn=Int();
String str="";
count(num,str,nn);
}
}
/**
*
* @param num 表示要排列的'數組
* @param str 以排列好的字符串
* @param nn 剩下需要排列的個數,如果需要全排列,則nn為數組長度
*/
private static void count(int[] num, String str, int nn) {
if(nn==0){
tln(str);
return;
}
for(int i=0;i<th;i++){
if(!f[i]){
continue;
}
f[i]=false;
count(num,str+num[i],nn-1);
f[i]=true;
}
}
}
-
經典Java基礎面試題
Java技術具有卓越的通用性、高效性、平台移植性和安全性,廣泛應用於PC、數據中心、遊戲控制枱、科學超級計算機、移動電話和互聯網,同時擁有全球最大的開發者專業社羣。下面小編給大家介紹經典Java基礎面試題,歡迎閲讀!經典Java基礎面試題1、如果main方法被聲明為...
-
計算機二級JAVA考試構建JAVA程序2017
很多人一見到説要編程就以為很難,其實只有一點一滴學知識點也不難,下面是小編整理的計算機二級JAVA考試構建JAVA程序介紹,歡迎閲讀!第一個javaapplicationjava程序分為javaapplication(java應用程序)和javaapplet(java小應用程序)兩種。下面讓我們編寫一個java應...
-
初學java編程入門必備的知識
基礎知識是新手學習一門新技術的必須掌握的,下面內容由小編為大家介紹初學java編程入門必備的知識,供大家參考!01、Java編程出現的背景1991年Sun公司的JamesGosling等人開始開發名稱為Oak的語言。希望用於控制嵌入在有線電視交換盒、PDA等的微處理器,但卻沒有取得...
-
JavaBeans 與 Ejb 的區別相關資料
導語:您現在可能已在使用Javabeans,但還不瞭解它。下面的是本站小編為大家蒐集的JavaBeans與Ejb的區別相關資料,供大家參考。Javabeans如果有支持Java的瀏覽器,那麼,在桌面上使用Javabeans就沒有限制。使用的Web頁面可以將beans作為小應用程序的一部分。您很快就會...