【问题标题】:searching in n 2-dimensional arrays在 n 个二维数组中搜索
【发布时间】:2011-08-26 22:56:55
【问题描述】:

帮助了解如何在 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]);
      }
    }
  }
}

【问题讨论】:

  • 我在代码中没有看到任何二维数组,还是我遗漏了什么?
  • @Harray true 这段代码中没有二维数组,但这里需要在二维数组上实现。我正在努力解决这个问题。

标签: java arrays algorithm loops


【解决方案1】:

以下是您需要的代码:

(解决方案包括递归,使您的问题变得更容易)

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 方法应该连续打印 numbers ArrayList 中的数字。 checkValue 是您要检查的值。

希望这会有所帮助....

如果您想对此算法进行任何澄清,请写信。

【讨论】:

  • 但是我正在寻找我们可以在哪里实现这个在 n arrys 这满足 2 或 3 阵列如果我有 6 那么这将不会锻炼。
  • 你想让表的数量可变吗???或者你在写代码的时候就知道表的数量吗?
  • 将读入数组的表数。 N个表将不知道。这就是这里的基础......
  • 我会尽力为您提供答案
【解决方案2】:

您可以将表格视为值列表。然后,如果你有 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]

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

【讨论】:

  • 您好宏,感谢您的帮助和溶胶,但我正在寻找乘法运算发生。这里我们得到的组合是加法。我希望你明白我的意思。
  • @user756742:哦,对不起!我很困惑。但是,即使对于产品,这个想法也是一样的。我已经更改了代码,因此现在与您的问题一致:递归过程为您提供了表中产品等于某个值的所有元素组合。看看,如果您有任何问题,请告诉我。
  • @ Macros - 我只是在你的代码上从 excel 中读取并打印 de coloum 和行名称。我遇到了一个问题,请为我提供有价值的解决方案。细胞[][] 新细胞=新细胞[200][200]; int newsheet = workbook1.getNumberOfSheets(); for (int q=1;q
  • @user756742:我很害怕在这里帮不了你:看来你正在使用一些 Java API 访问电子表格。可能workbook1 已经是null:检查这个。另外,sheet() 是一种方法吗?我相信您在这里还有另一个问题,与如何从 Java 中读取电子表格有关:这与您上面的问题无关;您可以考虑在读取电子表格的代码上提出一个新的特定问题。
  • @MAcros U R rite m 从电子表格中读取。感谢您的输入:) 将作为新 Q 发布...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多