有一组数A(一定大于3 有小数) 比如 3,5,30,50
另一组数B(一定大于0 有小数) 比如 1,2,3.5,4,10,20,30A每次取一个数 B每次可以取多个
现在要求取最优组合 结果小于等于0.3或者大于等于2
每个数字只能使用一次比方
A[0]-B[0]-B[1]=0  小于0.3 则为正确组合
A[1]-B[2]=1.5  则为错误组合
A B都允许有剩余未使用的数字 比如消耗完A 剩余B  或者消耗完B 剩余A
要求组合尽可能满足条件(小于等于0.3或者大于等于2)

解决方案 »

  1.   

    是不是说的不够清楚?
    重新写下
    有一组数A(一定大于3 有小数) 比如 3,5,30,50
    另一组数B(一定大于0 有小数) 比如 1,2,3.5,4,10,20,30A每次取一个数 B每次可以取多个
    现在要求取最优组合 结果 大于0小于等于0.3 或者 大于等于2(结果>0&&结果<=0.3  ||   结果>=2)
    每个数字只能使用一次比方
    A[0]-B[0]-B[1]=0 小于0.3 则为正确组合
    A[1]-B[2]=1.5 则为错误组合A B都允许有剩余未使用的数字
    要求组合尽可能满足条件
    求最优组合
      

  2.   


    import java.util.Arrays;
    public class Test {
    public static void main(String[] args) {
    double[] a = { 3, 5, 30, 50 };
    double[] b = { 1, 2, 3.5, 4, 10, 20, 30 };
    Arrays.sort(a);
    Arrays.sort(b);
    int arrIndex = 0;
    while (true && arrIndex < a.length) {
    double getA = a[arrIndex];
    int posIndex = 0;
    for (int i = b.length - 1; i >= 0; i--) {
    if (getA > b[i]) {
    posIndex = i;
    break;
    }
    }
    double sum = 0d;
    double result = 0d;
    for (int j = 0; j < posIndex; j++) {
    sum += b[j];
    result = getA - sum;
    if (sum != 0 && (result > 0 && result < 0.3) || result >= 2) {
    double[] arrB = Arrays.copyOfRange(b, 0, j+1);
    System.out.println("A:" + getA + "     " + "B:"
    + Arrays.toString(arrB));
    }
    }
    arrIndex++;
    }
    }
    }
      

  3.   

    有大于等于0.3了?
    import java.util.Arrays;
    public class Test {
    public static void main(String[] args) {
    double[] a = { 3, 5, 30, 50 };
    double[] b = { 1, 2, 3.5, 4, 10, 20, 30 };
    Arrays.sort(a);
    Arrays.sort(b);
    int arrIndex = 0;
    while (true && arrIndex < a.length) {
    double getA = a[arrIndex];
    int posIndex = 0;
    for (int i = b.length - 1; i >= 0; i--) {
    if (getA > b[i]) {
    posIndex = i;
    break;
    }
    }
    double sum = 0d;
    double result = 0d;
    for (int j = 0; j < posIndex; j++) {
    sum += b[j];
    result = getA - sum;
    if (sum != 0 && (result > 0 && result <=0.3) || result >= 2) {
    double[] arrB = Arrays.copyOfRange(b, 0, j+1);
    System.out.println("A:" + getA + "     " + "B:"
    + Arrays.toString(arrB));
    }
    }
    arrIndex++;
    }
    }
    }