导航:首页 > 编程大全 > java排列组合工具

java排列组合工具

发布时间:2023-02-21 10:26:01

1. java排列组合算法如题:用x、y,求出指定长度的所有排列组合。

按照你的要求编写的求x,y指定长度的所有排列组合的Java程序如下

importjava.util.ArrayList;
importjava.util.List;
publicclassEE{
publicstaticvoidcombination(List<String>list,StringsNumbers,StringsPath,intALen)
{
if(sPath.length()==ALen)
{
list.add(sPath);
return;
}
for(inti=0;i<sNumbers.length();i++)
{

combination(list,sNumbers,sPath+sNumbers.substring(i,i+1),ALen);
}
}
publicstaticvoidmain(String[]args){
List<String>output=newArrayList<String>();
System.out.println("组合");
combination(output,"xy","",5);
for(Strings:output)
System.out.print(s+"");
System.out.println();
System.out.println("共"+output.size()+"个");
}
}

运行结果

组合

xxxxx xxxxy xxxyx xxxyy xxyxx xxyxy xxyyx xxyyy xyxxx xyxxy xyxyx xyxyy xyyxx xyyxy xyyyx xyyyy yxxxx yxxxy yxxyx yxxyy yxyxx yxyxy yxyyx yxyyy yyxxx yyxxy yyxyx yyxyy yyyxx yyyxy yyyyx yyyyy

共32个

2. 用Java编写程序;求字母A和B的各种排列组合(递归思想)

//PS:不太明白函数中Strings的意义,所以忽略了。
importjava.util.ArrayList;
importjava.util.List;


publicclassCompoundString{

privatestaticvoidgenerate(intn,List<String>results){
if(n==1){
return;
}
List<String>resultsTemp_A=newArrayList<String>();
List<String>resultsTemp_B=newArrayList<String>();
for(Stringstr:results){
Stringtemp_A=str+"A";
resultsTemp_A.add(temp_A);
Stringtemp_B=str+"B";
resultsTemp_B.add(temp_B);
}
//此处results引用不能改变,否则任何修改对main函数中的results都无效
results.clear();
results.addAll(resultsTemp_A);
results.addAll(resultsTemp_B);
generate(n-1,results);//recursive
}

publicstaticvoidmain(String[]args){
List<String>results=newArrayList<String>();
results.add("A");
results.add("B");
generate(8,results);
for(Stringstr:results){
.out.println(str);
}
}
}

3. java 定义了5个数字的数组,显示输出所有的排列组合

数组排序有专门的API,这里就不说了

获取1-30的5个随机数即可

使数组的下标和里面的值相同,那么 可以通过 数组[26-当前下标] 是否被去除 来输出

int a[] = new int[31];

// 赋值,去除

for(int i=1;i<26/2;i++){

if(a[i]!=0 && a[26-i]!=0) 输出;

4. Java程序如何实现对字符串的排列组合问题

import java.math.BigInteger;
import java.util.*;

public class PermutationGenerator {

private int[] a;
private BigInteger numLeft;
private BigInteger total;
public PermutationGenerator(int n) {
if (n < 1) {
throw new IllegalArgumentException("Min 1");
}
a = new int[n];
total = getFactorial(n);
reset();
}

public void reset() {
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
numLeft = new BigInteger(total.toString());
}

public BigInteger getNumLeft() {
return numLeft;
}

public BigInteger getTotal() {
return total;
}

public boolean hasMore() {
return numLeft.compareTo(BigInteger.ZERO) == 1;
}

private static BigInteger getFactorial(int n) {
BigInteger fact = BigInteger.ONE;
for (int i = n; i > 1; i--) {
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
return fact;
}

public int[] getNext() {

if (numLeft.equals(total)) {
numLeft = numLeft.subtract(BigInteger.ONE);
return a;
}

int temp;

// Find largest index j with a[j] < a[j+1]

int j = a.length - 2;
while (a[j] > a[j + 1]) {
j--;
}

// Find index k such that a[k] is smallest integer
// greater than a[j] to the right of a[j]

int k = a.length - 1;
while (a[j] > a[k]) {
k--;
}

// Interchange a[j] and a[k]

temp = a[k];
a[k] = a[j];
a[j] = temp;

// Put tail end of permutation after jth position in increasing order

int r = a.length - 1;
int s = j + 1;

while (r > s) {
temp = a[s];
a[s] = a[r];
a[r] = temp;
r--;
s++;
}

numLeft = numLeft.subtract(BigInteger.ONE);
return a;

}
//程序测试入口
public static void main(String[] args) {

int[] indices;
String[] elements = { "a", "b", "c"};
PermutationGenerator x = new PermutationGenerator(elements.length);
StringBuffer permutation;

while (x.hasMore())
{
permutation = new StringBuffer("%");
indices = x.getNext();
for (int i = 0; i < indices.length; i++) {
permutation.append(elements[indices[i]]).append("%");
}
System.out.println(permutation.toString());

}
}

}

先给你一个看看!

5. 用java程序编写,1234这四个数进行排列组合,

程序已写出,希望对你有帮助。
import java.util.ArrayList;
import java.util.List;
public class Order {
public static String[] str = {"1","2","3","4"};
public static void main(String[] args){
System.out.println("-------------");
for(String s:str){
System.out.println(s+" , ");
}
System.out.println("-------------");
for(String s:get2byte()){
System.out.print(s+" , ");
}
System.out.println("-------------");
for(String s:get3byte()){
System.out.print(s+" , ");
}
System.out.println("-------------");
for(String s:get4byte()){
System.out.print(s+" , ");
}
}
public static List<String> get2byte(){
List<String> list = new ArrayList<String>();
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length;j++){
list.add(str[i]+str[j]);
}
}
return list;
}
public static List<String> get3byte(){
List<String> list = new ArrayList<String>();
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length;j++){
for(int k=0;k<str.length;k++){
list.add(str[i]+str[j]+str[k]);
}
}
}
return list;
}
public static List<String> get4byte(){
List<String> list = new ArrayList<String>();
for(int i=0;i<str.length;i++){
for(int j=0;j<str.length;j++){
for(int k=0;k<str.length;k++){
for(int h=0;h<str.length;h++){
list.add(str[i]+str[j]+str[k]+str[h]);
}
}
}
}
return list;
}
}

阅读全文

与java排列组合工具相关的资料

热点内容
任命文件人字多少号 浏览:27
如何给桌面的文件自动填写序号 浏览:349
压缩文件JPG重命名 浏览:487
vivox7怎么隐藏文件 浏览:340
官网买苹果好处 浏览:153
直播app源码如何搭建 浏览:31
c盘系统文件包介绍图片 浏览:856
linuxopt主要存哪些文件 浏览:924
jsdate天数 浏览:930
qq空间怎样发视频文件在哪里找 浏览:208
nrf2401收发程序 浏览:498
哪个网站看新闻最好 浏览:944
找到文件夹并清除linux 浏览:82
苹果app不安全怎么办 浏览:1000
电脑桌面文件收集软件推荐 浏览:317
桌面的文件夹不能删除不了怎么办 浏览:724
什么是手机温控文件 浏览:840
广东农信app怎么更改预留手机号码 浏览:241
cdm打开隐藏文件夹 浏览:320
贝壳app房子为什么偶尔不显示 浏览:4

友情链接