在 n 个二维数组中搜索
帮助了解如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是您需要的代码:(
解决方案包括递归,使您的问题变得更容易)
您需要实现一个类(“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)
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.
您可以将表视为值列表。然后,如果您有 N 个表,您的问题是找到 N 个整数的列表(每个整数取自 N 个表之一),其乘积等于值 p。您可以递归地解决该问题:
{t1, t2, t3, ...}
p
t1
中的每个值v
都必须寻找具有乘积值p / v
和表{ 的子问题的解决方案t2,t3,...}
(这假设p % v == 0
,因为我们正在处理整数下面是一些java代码:
该代码打印示例的稍微修改版本的解决方案:
该解决方案适用于任何有很多方法可以改进算法,例如使用记忆和动态规划。
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:
{t1, t2, t3, ...}
p
that you look forv
int1
you must look for solutions of the sub-problem with a product valuep / v
and and tables{t2, t3, ...}
(this assumes thatp % v == 0
, because we're dealing with integersSome java code below:
The code prints solutions for a slightly modified version of your example:
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.