在 n 个二维数组中搜索

发布于 2024-11-07 20:10:17 字数 533 浏览 0 评论 0原文

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

Help with how to implement searching on n 2-dimenssional arrays. To be more specific:
If I have 6 tables and I am putting these into a 2-dimensional array.I will provide a value say 10 like how val=0 here. I need to search from these tables all the combination values that make up 10. The value will be computed taking values from all these tables.

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]);
      }
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2024-11-14 20:10:17

以下是您需要的代码:(

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

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 是您要检查的值。

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

如果您想对此算法有任何说明,请写信。

Following will be the code you require:

(The solution includes recursion making your problem go easier)

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();
                }
            }
        }
    }        
}

You need to implement a class ('Tables' as my solution) write methods:

boolean isLast(int tableNo): to check whether the given table is the last table your tables list

int[][] Get(int tableNo): to get the table with the specified index

Also the method sum should sum the values in the numbers ArrayList.
PrintNumbers method should print the numbers in the numbers ArrayList in a row.
checkValue is the value you want to check.

Hope this helps....

Please write if you want any clarification on this algorithm.

软糖 2024-11-14 20:10:17

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

该解决方案适用于任何有很多方法可以改进算法,例如使用记忆和动态规划。

You can consider a table as list of values. Then, if you have N tables, your problem is to find lists of N integers (each one taken from one of the N tables) whose product is equal to a value p. You can solve the problem recursively:

  • given a non-empty list of tables {t1, t2, t3, ...}
  • given a product value p that you look for
  • for every value v in t1 you must look for solutions of the sub-problem with a product value p / v and and tables {t2, t3, ...} (this assumes that p % v == 0, because we're dealing with integers
  • etc.

Some java code below:

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;
    }

}

The code prints solutions for a slightly modified version of your example:

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

The solutions work for any number of tables. There are ways to improve the algorithm, for example by using memoization and dynamic programming. But I think that the recursive solution is more clear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文