在n个二维数组中进行搜索

3

如何在n个二维数组上实现搜索,具体来说: 假设我有6张表格,并将它们放入一个二维数组中。我会提供一个值,比如10,就像这里的val=0一样。我需要从这些表格中搜索所有组成10的组合值。这个值将通过从所有这些表格中获取值进行计算。

public static int Main() {
  int[] a = {2,1,4,7};
  int[] b = {3,-3,-8,0};
  int[] c = {-1,-4,-7,6};
  int sum;
  int i; int j;  int k;
  int val = 0;
  for(i = 0; i < 4; i++) {
    for(j = 0;j<4;j++) {
      for(k = 0;k<4;k++) {
        sum = a[i]* b[j]* c[k];

        if(sum == val)
          System.out.printf("%d  %d  %d\n",a[i],b[j],c[k]);
      }
    }
  }
}

我在代码中没有看到任何二维数组,或者是我漏看了什么? - Harry Joy
@ Harray true,这段代码中没有二维数组,但这里需要使用二维数组进行实现。我正在努力解决这个问题。 - user756742
2个回答

2
以下是您需要的代码:
(该解决方案包括递归,使您的问题更加容易)
注:保留了HTML标签。
private ArrayList numbers = new ArrayList();

public void CalculateSum(int tableNumber)
{
    if(!Tables.isLast(tableNumber))
    {
        int[][] a = Tables.Get(tableNumber);
        for(int y = 0; y < a.length; y++)
        {
            for(int x = 0; x < a[y].length; x++)
            {
                numbers.add(a[y][x]);
                CalculateSum(tableNumber + 1);
                numbers.remove(tableNumber - 1);
            }
        }
    }else
    {
        int[][] a = Tables.Get(tableNumber);
        for(int y = 0; y < a.length; y++)
        {
            for(int x = 0; x < a[y].length; x++)
            {
                if((sum(numbers) + a[y][x]) == checkValue)
                {
                    PrintNumbers(numbers);
                    System.out.print(a[y][x]);
                    System.out.println();
                }
            }
        }
    }        
}

你需要实现一个类(我的解决方案是'Tables'),编写以下方法:
boolean isLast(int tableNo):检查给定的表是否是您的表列表中的最后一张表。
int[][] Get(int tableNo):获取具有指定索引的表。
另外,方法sum应该对数字ArrayList中的值进行求和。 PrintNumbers方法应该将数字ArrayList中的数字打印在一行中。 checkValue是您想要检查的值。
希望这能帮助你...... 如果您需要任何澄清,请写信告诉我。

但是我正在寻找我们可以在n个数组上实现此操作的位置,这对于2或3个数组是可行的,如果我有6个数组,那么这将无法实现。 - user756742
你想要表的数量是可变的吗?还是在编写代码时就确切地知道表的数量? - Dulini Atapattu
表格的数量将被读入数组中。表格的数量是未知的。这就是本质所在... - user756742
我会尽力为您提供答案。 - Dulini Atapattu

0

你可以将一个表格视为值的列表。然后,如果你有N个表格,你的问题就是找到N个整数列表(每个整数来自于N个表格中的一个),其乘积等于一个值p。你可以递归地解决这个问题:

  • 给定一个非空的表格列表 {t1、 t2、 t3、...}
  • 给定要查找的乘积值 p
  • 对于t1中的每个值v,您必须寻找具有乘积值p/v和表格{t2、 t3、...} 的子问题的解决方案(这假设p%v==0,因为我们处理整数
  • 等等。

一些Java代码如下:

public class SO6026472 {

    public static void main(String[] args) {
        // define individual tables
        Integer[] t1 = new Integer[] {2,-2,4,7};
        Integer[] t2 = new Integer[] {3,-3,-8,0};
        Integer[] t3 = new Integer[] {-1,-4,-7,6};
        Integer[] t4 = new Integer[] {1,5};
        // build list of tables
        List<List<Integer>> tables = new ArrayList<List<Integer>>();
        tables.add(Arrays.asList(t1));
        tables.add(Arrays.asList(t2));
        tables.add(Arrays.asList(t3));
        tables.add(Arrays.asList(t4));
        // find solutions
        SO6026472 c = new SO6026472();
        List<List<Integer>> solutions = c.find(36, tables);
        for (List<Integer> solution : solutions) {
            System.out.println(
                    Arrays.toString(solution.toArray(new Integer[0])));
        }
    }

    /**
     * Computes the ways of computing p as a product of elements taken from 
     * every table in tables.
     * 
     * @param p the target product value
     * @param tables the list of tables
     * @return the list of combinations of elements (one from each table) whose
     * product is equal to p
     */
    public List<List<Integer>> find(int p, List<List<Integer>> tables) {
        List<List<Integer>> solutions = new ArrayList<List<Integer>>();
        // if we have no tables, then we are done
        if (tables.size() == 0)
            return solutions;
        // if we have just one table, then we just have to check if it contains p
        if (tables.size() == 1) {
            if (tables.get(0).contains(p)) {
                List<Integer> solution = new ArrayList<Integer>();
                solution.add(p);
                solutions.add(solution);
                return solutions;
            } else
                return solutions;
        }
        // if we have several tables, then we take the first table T, and for
        // every value v in T we search for (p / v) in the rest of the tables;
        // we do this only if p % v is equal to 0, because we're dealing with
        // ints
        List<Integer> table = tables.remove(0);
        for (Integer value : table) {
            if (value != 0 && p % value == 0) {
                List<List<Integer>> subSolutions = find(p / value, tables);
                if (! subSolutions.isEmpty()) {
                    for (List<Integer> subSolution : subSolutions) {
                        subSolution.add(0, value);
                    }
                    solutions.addAll(subSolutions);
                }
            }
        }
        tables.add(0, table);
        return solutions;
    }

}

这段代码打印了一个稍微修改过的示例的解决方案:

[2, 3, 6, 1]
[-2, -3, 6, 1]

这些解决方案适用于任意数量的表格。有一些方法可以改进算法,例如使用记忆化和动态规划。但我认为递归解决方案更加清晰。


嗨,Macros,感谢您的帮助和解决方案,但我正在寻找乘法操作。在这里,我们得到的组合是加法。我希望你明白我的意思。 - user756742
@user756742:哦,抱歉!我有点混淆了。不过,即使是针对产品,这个想法也是一样的。我已经修改了我的代码,现在它与你的问题保持一致:递归过程会给出所有元素组合的表格,其乘积等于某个特定值。看一下并让我知道如果你有问题。 - MarcoS
@宏 - 我刚刚查看了您的代码,从Excel中读取并打印列和行名称。我遇到了一个问题,请提供您宝贵的解决方案。 Cell [] [] newcell = new Cell [200] [200]; int newsheet = workbook1.getNumberOfSheets(); for(int q = 1; q <newsheet; q ++) { for(int p = 0; p <sheet(q).getColumns(); p ++) { for(int p1 = 0; p1 <sheet(q).getRows(); p1 ++) ( newcell [p] [p1] = sheet(q).getCell(p,p1); if(newcell [p] [p1] .equals(saved [j])) { System.out.print(newcell [p] [0]); } } } 因为它引发了NullPointer异常,所以我能否使用sheet()的属性作为sheet(q)? - user756742
@user756742:恐怕我无法在这里帮助你:看起来您正在使用一些Java API访问电子表格。可能workbook1已经是null了:请检查一下。此外,sheet()是一个方法吗?我认为您在这里还有另一个问题,与如何从Java读取电子表格有关:这与您上面的问题无关;您可以考虑针对读取电子表格的代码提出一个新的具体问题。 - MarcoS
@宏,你说得对,我正在从电子表格中读取数据。感谢您的建议:) 我会将其发布为新问题... - user756742

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接