还记得排列组合的话试试排列组合吧,我没有想到java里面有什么特别的方法来解决这个问题,下面的代码作为参考: package test;import junit.framework.TestCase;public class Test2 extends TestCase { String[] a = { "A", "B", "C", "D", "E" }; int b = 4; int l = a.length;//数组的长度 int c = l - b; // 排列,就是有序的 public void testPermutation() { System.out.println(factorial(l) / factorial(c)); } // 组合,无序的 public void testCombination() { System.out.println(factorial(l) / (factorial(b) * factorial(c))); } public static int factorial(int x) { if (x < 0) { throw new IllegalArgumentException("x must be>=0"); } if (x <= 1) { return 1; } else return x * factorial(x - 1); } } 我不知道有没有理解你的意思,姑且看看。有什么错误请告之。谢谢
1)第1个是组合(设:String [] a的长度为m),是m中取b个元素的组合。
2)第2个是排列(设:String [] a的长度为m),是m中取b个元素的排列。
这两个的算法,到处都能找到。
package test;import junit.framework.TestCase;public class Test2 extends TestCase
{
String[] a =
{ "A", "B", "C", "D", "E" };
int b = 4;
int l = a.length;//数组的长度
int c = l - b;
// 排列,就是有序的
public void testPermutation()
{
System.out.println(factorial(l) / factorial(c));
} // 组合,无序的
public void testCombination()
{
System.out.println(factorial(l) / (factorial(b) * factorial(c)));
} public static int factorial(int x)
{
if (x < 0)
{
throw new IllegalArgumentException("x must be>=0");
}
if (x <= 1)
{
return 1;
} else
return x * factorial(x - 1);
}
}
我不知道有没有理解你的意思,姑且看看。有什么错误请告之。谢谢