【问题标题】:Finding all possible Combination of Pairs in a list在列表中查找所有可能的对组合
【发布时间】:2013-06-21 16:33:10
【问题描述】:

我正在尝试按部分实现集成,并尝试从所有相乘的列表中提取所有可能的 udv 对。提供的代码是我的方法,除了 Function 对象已替换为字符串以提高可读性,以便人们可以脱离上下文运行它。

ArrayList<String> funcObjects = giveList(CALC.MULTIPLY, function);//populate this any way you like

//all the pairs are stored in a matrix
int pairCounter = 0;
String[][] udvPairs = new String[funcObjects.size() * funcObjects.size()][2];
//for (int skip = 0; skip < 1; skip++) {
//commented out for sake of a better solution
for (int i = 0; i < funcObjects.size() - 1; i++) {
    System.out.println("i=" + i);
    //System.out.println(function.size());
    for (int j = 0; j < funcObjects.size() - i; j++) {
        System.out.println("j=" + j);
        CalcObject u = "1";
        CalcObject dv = "1";
        for (int start = j; start <= j + i; start++) {
            //this loop here is what is generating my u.
            //note that it goes in order and therefore cannot
            //account for items that are not next to each other in the list
            //my question is how to add a fix for this
            u = u + " * " + funcObjects.get(start);
        }
        for (int end = 0; end < j; end++) {
            dv = dv + " * " + funcObjects.get(end);
        }
        for (int end = j + i + 1; end < funcObjects.size(); end++) {
            dv = dv + " * " + funcObjects.get(end);
        }

        System.out.println("Pair " + pairCounter + "; u: " + u.toString() + " dv: " + dv.toString());
        udvPairs[pairCounter][0] = u;
        udvPairs[pairCounter][1] = dv;
        pairCounter++;
    }
}

到目前为止,这是我的代码。它给我的组合是正确的,但它并没有给我所有的组合。例如:

x * SIN(x) * COS(x)

即传入的列表是["x","SIN(x)","COS(x)"]

会给我

i=0  
j=0  
Pair 0; u: x dv: SIN(x) * COS(x)  
j=1   
Pair 1; u: SIN(x) dv: x * COS(x)  
j=2  
Pair 2; u: COS(x) dv: x * SIN(x)  
i=1  
j=0  
Pair 3; u: x * SIN(x) dv: COS(x)  
j=1  
Pair 4; u: SIN(x) * COS(x) dv: x  

它正在跳过你:x * COS(x) dv: SIN(x)

所以我的问题是,任何人都知道如何让它也考虑到部件不相邻的组合?程序没有抛出任何错误,我只是不知道如何完成我需要的实现。

谢谢。

【问题讨论】:

  • ij 的哪些值对应于您缺少的案例?
  • i 必须为 1 才能为 u 提供 2 个字符串,而 j 只是在从索引 j 开始计数的位置递增。
  • 我想问题是为什么你没有达到ij 的正确值。 (我正在努力帮助您解决自己的问题。;))
  • 它不一定是错误的值...
  • 我想通了,我猜问题已经结束了。当烦人的 10 小时计时器用完时,我会回答我自己的问题。

标签: java permutation combinations calculus


【解决方案1】:

这是使它工作的代码:

ArrayList<String[]> udvPairs = new ArrayList<>();
String[] temp = new String[2];
String notOne = "1";
for (int i = 0; i < funcObjects.size(); i++) {
    notOne = notOne += funcObjects.get(i);
}
temp[0] = "1";
temp[1] = notOne;
udvPairs.add(temp);
temp = new String[2];
temp[1] = "1";
temp[0] = notOne;
udvPairs.add(temp);
for (int i = 0; i < funcObjects.size() - 1; i++) {
    //System.out.println("i=" + i);
    //System.out.println(function.size());
    for (int j = 0; j < funcObjects.size() - i; j++) {
        //System.out.println("j=" + j);
        for (int skip = 0; skip < funcObjects.size() - i - j; skip++) {
            //System.out.println("skip=" + skip);
            String u = "1";
            String dv = "1";
            u += funcObjects.get(j);
            for (int start = j + skip + 1; start <= j + i + skip; start++) {
                u += ufuncObjects.get(start);
            }
            for (int end = 0; end < j; end++) {
                dv += funcObjects.get(end);
            }
            for (int end = j + 1; end < j + skip + 1; end++) {
                dv += funcObjects.get(end);
            }
            for (int end = j + i + 1 + skip; end < funcObjects.size(); end++) {
                dv += funcObjects.get(end);
            }
            //}
            //System.out.println("Pair " + pairCounter + "; u: " + u.toString() + " dv: " + dv.toString());
            temp = new String[2];
            temp[0] = u;
            temp[1] = dv;
            boolean addIt = true;
            for (int x = 0; x < udvPairs.size(); x++) {
                if (udvPairs.get(x)[0].equals(u) && udvPairs.get(x)[1].equals(dv)) {
                    addIt = false;
                    x = udvPairs.size();
                }
            }
            if (addIt) {
                udvPairs.add(temp);
            }
            //pairCounter++;
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2019-06-04
    • 2017-01-22
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多