想用java运算一下
a>=3 ,取5
b>=3, 取3
c>=3, 取2
a+b>=3,取3
其它取2
同时满足a+b+c=5
想知道有几种组合方式?
你说的这个取5,取3是啥意思,什么东西取,就暂且认为你是返回的意思
public static void main(String[] args) {
int[] an = {0, 1, 2, 3, 4, 5};
int[] bn = {0, 1, 2, 3, 4, 5};
int[] cn = {0, 1, 2, 3, 4, 5};
for (int a : an) {
for (int b : bn) {
for (int c : cn) {
if (a + b + c == 5) {
int res = calculate(a, b, c);
System.out.println(res);
}
}
}
}
}
private static int calculate(int a, int b, int c) {
if (a >= 3) {
return 5;
}
if (b >= 3) {
return 3;
}
if (c >= 3) {
return 2;
}
if (a + b >= 3) {
return 3;
} else {
return 2;
}
}
没看懂你要表达什么?能说的明白点吗?
List<String> res = new ArrayList<>();
public int searchCom(int[] a, int[] b, int[] c) {
Arrays.sort(a);
Arrays.sort(b);
Arrays.sort(c);
System.out.println("===========================[a]=======================\n");
Arrays.stream(a).forEach(x->System.out.print(x+"\t"));
System.out.println("\n\n===========================[b]=======================\n");
Arrays.stream(b).forEach(x->System.out.print(x+"\t"));
System.out.println("\n\n===========================[c]=======================\n");
Arrays.stream(c).forEach(x->System.out.print(x+"\t"));
System.out.println("\n\n=====================================================\n");
int result = 0;
int indexC = -1;
int maxA = a[a.length-1];
int maxB = b[b.length-1];
int maxC = c[c.length-1];
int startC = 0;
for (int k = 0; k < c.length; k++) {
if (c[k] > 1) {
indexC = k;
startC = c[k];
break;
}
}
if (indexC == -1) return result;
int lenC = c.length - indexC;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {
boolean continueCal = continueCal(a[i], b[j]);
if (continueCal) {
int lenB = b.length - j;
if(j== 0 || ( !continueCal(maxA, b[j-1]))){
int lenA = a.length - i;
int cur = lenA * lenB * lenC;
result+= cur;
res.add("a 取值范围:["+a[i]+" ~ "+maxA+"], b 取值范围:["+b[j]+" ~ "+maxB+"], c 取值范围:["+startC+" ~ "+maxC+"],组合数count="+cur);
return result;
}else{
int cur = lenC * lenB;
result += cur;
res.add("a="+a[i]+", b 取值范围:["+b[j]+" ~ "+maxB+"], c 取值范围:["+startC+" ~ "+maxC+"],组合数:count="+cur);
break;
}
}
}
}
return result;
}
boolean continueCal(int a, int b) {
if (a > 2) a = 5;
if (b > 2) b = 3;
return a + b > 2;
}
测试:
@Test
public void test() {
int result = searchCom(
new int[]{0, -16, 92, 63, 84, 5, 6,78,456,-67},
new int[]{0, -1, -26, 3, 4, 5, 6, 7,7878,678,234,789,90,-7864},
new int[]{0, 1, 2, 3, 4, 5, 6, 7,-98,-676,-88,-90});
System.out.println("满足条件的ABC组合总数:"+result);
res.forEach(r->System.out.print(r+"\n"));
}
结果:
===========================[a]=======================
-67 -16 0 5 6 63 78 84 92 456
===========================[b]=======================
-7864 -26 -1 0 3 4 5 6 7 90 234 678 789 7878
===========================[c]=======================
-676 -98 -90 -88 0 1 2 3 4 5 6 7
=====================================================
满足条件的ABC组合总数:564
a=0, b 取值范围:[3 ~ 7878], c 取值范围:[2 ~ 7],abc组合数:count=60
a 取值范围:[5 ~ 456], b 取值范围:[-1 ~ 7878], c 取值范围:[2 ~ 7],abc组合数count=504