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语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。下面小编整理了java语言运算符的使用,希望对大家有帮助!赋值运算符的使用=、+=、-=、*=、/=、%=算术运算符的使用算术运算就是咱们生活中常见的加、...
-
在Java里处理文件的技巧
导读:看到太多的凌乱的,不安全的处理文件的代码了。可以说每个项目都会有人喜欢写自己的一些FileUitl。下面是小编为大家整理的在Java里处理文件的技巧,欢迎参考~实用的工具类,Path,Paths,Files,FileSystem有一些很灵活的处理方法://得到一个Path对象Pathpath=("/test/...
-
Java程序设计示例教程
本文以实例形式详细讲述了Java的反射机制,是Java程序设计中重要的技巧。分享给大家供大家参考。具体分析如下:首先,Reflection是Java程序开发语言的特征之一,它允许运行中的Java程序对自身进行检查,或者说"自审",并能直接操作程序的内部属性。例如,使用它能获得Java类...
-
计算机二级Java备考习题及答案
练习可以帮助我们加深对知识的记忆和理解,下面是本站小编整理的2017计算机二级Java备考练习题及答案,欢迎学习!备考练习题一1、下列叙述中,错误的是______。A、Applet的默认布局管理器是FlowLayoutB、JApplet中增加构件是加到JApplet的内容面板上,不是直接加到JApp...