有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

n2d数组中的java搜索

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

共 (2) 个答案

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

    您需要实现一个类(“表”作为我的解决方案)写入方法:

    布尔isLast(int tableNo):检查给定表是否是表列表中的最后一个表

    int[][]Get(int tableNo):获取具有指定索引的表

    方法sum也应该对numbers数组列表中的值求和。 PrintNumbers方法应将numbers ArrayList中的数字打印成一行。 checkValue是要检查的值

    希望这有帮助

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

  2. # 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]
    

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