java组合请求
三个整数数组a[],b[],c[]。数组长度是可变的,每个数组中的数字不能重复。a[]中的数字取自0到13,长度至少为9,b[]中的数字取自a[],c[]中的数字取自b[]。从三个数组中取9个数字,每个数字在每个数组中只能取1次,得到数字的组合 例1:java组合请求,java,arrays,Java,Arrays,三个整数数组a[],b[],c[]。数组长度是可变的,每个数组中的数字不能重复。a[]中的数字取自0到13,长度至少为9,b[]中的数字取自a[],c[]中的数字取自b[]。从三个数组中取9个数字,每个数字在每个数组中只能取1次,得到数字的组合 例1: a[]={0,1,2,3,4,5,6,7,8} b[]={1,2} c[]={1} return totalCount=6; 因为('a'表示来自数组a[]) 例2: a[]={0,1,2,3,4,5,6,7,8,9,10,11,12
a[]={0,1,2,3,4,5,6,7,8}
b[]={1,2}
c[]={1}
return totalCount=6;
因为('a'表示来自数组a[])
例2:
a[]={0,1,2,3,4,5,6,7,8,9,10,11,12}
b[]={0,1,2,3,4}
c[]={0,1,2,3,4}
return totalCount=47985
我做了一些工作,但它只在c[]的长度为0或a[]==9时起作用
int num=0;
if(a.size()<9){
num = 0;
}else{
if(b.size()==0){
num= combineNum(a.size(),9).intValue();
}else{
if(three.size()==0){
num=combineNum(a.size(),9).intValue();
for(int i=1;i<=b.size();i++){
num=num+combineNum(a.size()-i,9-i).intValue()
*combineNum(b.size(),b.size()-i).intValue();
}
}else{
if(c.size()==b.size()){
if(a.size()==9){
num=(int)Math.pow(3,c.size());
}else{
num=combineNum(a.size(),9).intValue();
for(int i=1;i<=b.size();i++){
if(i==1){
num=num+combineNum(a.size()-i,9-i).intValue()
*combineNum(2*b.size(),i).intValue();
}else{
num=num+combineNum(a.size()-i,9-i).intValue()
*(combineNum(2*b.size(),i).intValue()-
b.size()*combineNum(2*b.size()-2*(i-1),i-1).intValue());
}
}
}
}else {
if(a.size()==9){
num= (int)(Math.pow(3,c.size())*Math.pow(2,b.size()-c.size())
*combineNum(a.size(),9).intValue());
}else{
num=-1;
}
}
}
}
}
return num;
int num=0;
如果(a.size()我已经解决了这个问题。
把我的代码放在下面
public static long getRen9Numbers(boolean[] winChecked,
boolean[] tieChecked,boolean[] lossChecked) throws Exception {
TreeSet<Integer> first=new TreeSet<>();
TreeSet<Integer> second=new TreeSet<>();
TreeSet<Integer> three=new TreeSet<>();
for(int i=0;i<winChecked.length;i++){
if(winChecked[i]){
first.add(i);
}
}
for(int i=0;i<tieChecked.length;i++){
if(tieChecked[i]){
if(first.contains(i)){
second.add(i);
}else{
first.add(i);
}
}
}
for(int i=0;i<lossChecked.length;i++){
if(lossChecked[i]){
if(first.contains(i)){
if(second.contains(i)){
three.add(i);
}else{
second.add(i);
}
}else{
first.add(i);
}
}
}
long num=0;
if(first.size()<9){
num = 0;
}else{
if(second.size()==0){
num= combineNum(first.size(),9).intValue();
}else{
if(three.size()==0){
for(int i=0;i<=second.size();i++){
num=num+combineNum(second.size(),i).longValue()*
combineNum(first.size()-i,9-i).longValue();
}
}else{
for(int i=0;i<=three.size();i++){
long count1=0;
for(int j=0;j<=second.size()-i;j++){
count1=count1+combineNum(second.size()-i,j).longValue()*
combineNum(first.size()-(j+i),9-(j+i)).longValue();
}
num=num+combineNum(three.size(),i).longValue()*count1;
}
}
}
}
return num;
}
public static long getRen9Numbers(boolean[]winChecked,
布尔[]tieChecked,布尔[]lossChecked)引发异常{
TreeSet first=新树集();
树集秒=新树集();
树集三=新树集();
对于(inti=0;i计算每个数字重复多少次并将它们相乘,这是非常简单的数学
a[]={0,1,2,3,4,5,6,7,8}
b[]={1,2}
c[]={1}
您有3个1和2个2,因此您将有:
1(0)*3(可能的人)*2(可能的两人)*1(3)*1(4)*1(5)*1(6)*1(7)*1(8)=6你有问题吗?我投票把这个问题作为离题题题来结束,因为OP没有亲自解决这个问题,这是一个家庭作业堆。不,这不简单,首先,我需要从三个数组中选择9个数字,其次,9个数字不能被取,最后,a[]的长度可以是9~13,b的长度可以是[]或c[]是可变的因为数组没有包含方法,所以您有两个选项:1.对第一个数组执行a操作以获取其数字,并对两个嵌套操作以检查b或c是否包含数字。2.List aList=Arrays.asList(a);(与b和c相同)并在列表上迭代检查bList.contains(number);(或cList)您将需要两个变量,一个用于单个for iteration count,另一个用于结果为total*=iterationcount当长度变大@AlDeguer时,我无法获得所有的组合,就像示例2一样事实上,您确定示例2是正确的吗?我想结果应该是243,因为您有3*3*3*3*1*1*1*1*1*1
public static long getRen9Numbers(boolean[] winChecked,
boolean[] tieChecked,boolean[] lossChecked) throws Exception {
TreeSet<Integer> first=new TreeSet<>();
TreeSet<Integer> second=new TreeSet<>();
TreeSet<Integer> three=new TreeSet<>();
for(int i=0;i<winChecked.length;i++){
if(winChecked[i]){
first.add(i);
}
}
for(int i=0;i<tieChecked.length;i++){
if(tieChecked[i]){
if(first.contains(i)){
second.add(i);
}else{
first.add(i);
}
}
}
for(int i=0;i<lossChecked.length;i++){
if(lossChecked[i]){
if(first.contains(i)){
if(second.contains(i)){
three.add(i);
}else{
second.add(i);
}
}else{
first.add(i);
}
}
}
long num=0;
if(first.size()<9){
num = 0;
}else{
if(second.size()==0){
num= combineNum(first.size(),9).intValue();
}else{
if(three.size()==0){
for(int i=0;i<=second.size();i++){
num=num+combineNum(second.size(),i).longValue()*
combineNum(first.size()-i,9-i).longValue();
}
}else{
for(int i=0;i<=three.size();i++){
long count1=0;
for(int j=0;j<=second.size()-i;j++){
count1=count1+combineNum(second.size()-i,j).longValue()*
combineNum(first.size()-(j+i),9-(j+i)).longValue();
}
num=num+combineNum(three.size(),i).longValue()*count1;
}
}
}
}
return num;
}