Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 从给定数字计算唯一值_Algorithm - Fatal编程技术网

Algorithm 从给定数字计算唯一值

Algorithm 从给定数字计算唯一值,algorithm,Algorithm,假设我有6个随机数,我想从这些数中计算出一些唯一的值 编辑: 允许的操作有+、-、*、和/。每个数字只能使用一次。你不必使用所有的数字 示例: Given numbers: 3, 6, 100, 50, 25, 75 Requested result: 953 3 + 6 = 9 9 * 100 = 900 900 + 50 = 950 75 / 25 = 3 3 + 950 = 953 写一个解决这个问题的程序,最简单的算法方法是什么?最简单的方法是全部尝试:你有六个数字,这意味着最多有五

假设我有6个随机数,我想从这些数中计算出一些唯一的值

编辑: 允许的操作有+、-、*、和/。每个数字只能使用一次。你不必使用所有的数字

示例:

Given numbers: 3, 6, 100, 50, 25, 75
Requested result: 953

3 + 6 = 9
9 * 100 = 900
900 + 50 = 950
75 / 25 = 3
3 + 950 = 953

写一个解决这个问题的程序,最简单的算法方法是什么?

最简单的方法是全部尝试:你有六个数字,这意味着最多有五个点可以放置一个操作符,最多是
6排列。考虑到只有四个操作符,您需要通过
6*4^5或737280可能性。这可以通过递归函数,甚至嵌套循环轻松完成。根据语言的不同,您可以使用库函数来处理排列

与语言无关的递归方法要求您定义三个函数:

int calc(int nums[6], int ops[5], int countNums) {
    // Calculate the results for a given sequence of numbers
    // with the specified operators.
    // nums are your numbers; only countNums need to be used
    // ops are your operators; only countNums-1 need to be used
    // countNums is the number of items to use; it must be from 1 to 6
}

void permutations(int nums[6], int perm[6], int pos) {
    // Produces all permutations of the original numbers
    // nums are the original numbers
    // perm, 0 through pos, is the indexes of nums used in the permutation so far
    // pos, is the number of perm items filled so far
}

void solveRecursive(int numPerm[6], int permLen, int ops[5], int pos) {
    // Tries all combinations of operations on the given permutation.
    // numPermis the permutation of the original numbers
    // permLen is the number of items used in the permutation
    // ops 0 through pos are operators to be placed between elements
    // of the permutation
    // pos is the number of operators provided so far.
}
当您提到“唯一数字”时,假设您指的是使用手头所有数字生成的可能结果宇宙中的一个结果


如果是这样的话,为什么不先尝试所有运算符和可用数字的排列呢?

我认为最简单的算法方法是。它很容易实现,如果存在解决方案,它将始终找到解决方案。基本思想是递归的:在构建解决方案的每一步都做出任意选择,然后从那里开始。如果不成功,请尝试其他选择。当您没有选择时,向上一个选择点报告失败(或者如果没有上一个选择点,则报告找不到解决方案)


您的选择是:将涉及多少个数字,每个数字是什么(每个数字位置的一个选择),以及它们如何由运算符连接(每个运算符位置的一个选择)。

使用递归来排列数字和运算符。它是O(6!*4^5)

如果您想保证从这些数字生成一个唯一的数字,而不可能从不同的数字集中得到相同的数字,那么您应该使用基数算术,类似于十进制、十六进制等

但是你需要知道这些数字的最大值


基本上,它将是
A+B*MAX_A+C*MAX_A*MAX_B+D*MAX_A*MAX_B*MAX_C+E*MAX_A*MAX_B*MAX_C*MAX_D+F*MAX_A*。*MAX_E

那么你可以重复使用数字了吗?@jozefg它似乎没有重复使用数字。((3+6)*100)+50)+75/25=953.允许对数字进行哪些操作?只是
+
-
*
/
,或者像幂(例如,3^6),根,甚至连在一起(36)?哦,我忘了指定它。每个数字只能使用一次。允许的操作:+、-、*,和/或你在不久的将来要倒计时吗?不完全是:他不想按特定的顺序使用数字,而是想全部使用。这将添加多个案例(首先选择1个数字,然后选择一个运算符,计算。然后在剩余的数字中选择1,选择一个运算符,计算等)