问题描述
我正在尝试用javascript编写一个程序,该程序从html文本区域中获取未指定数量的数字,然后尝试所有组合(将所有数字相互添加)以查看它是否计算出您指定的数字。
现在我可以在textarea的字符串中制作一个数组,并使用 for
循环将它们加起来(请参见下面的代码)。问题是如何对未指定数量的要累加的数字执行此操作(例如,如果您在文本区域中输入7个数字,则要累加7个不同的数字)?我当时在考虑使用第二个数组,它可以将数字累加到第一个循环之外。然后通过使用for循环,使循环变量的长度与包含所有数字(在我的示例中为行)的数组的长度作为最终值。
如何填充第二个数组的值,确保使用所有组合?
顺便说一句,我想要这段代码,因为我是一名审核员。有时,客户会在一次预订中冲销几笔款项,而没有任何评论。此代码将使检查已撤销的预订变得更加容易
编辑:cheeken的遮篷似乎很有效,我只有一句话。如果您的功率设置中的多个子集相加会导致您要查找的数字怎么办?例如:findSum([1,2,3,4,5],6)可以得到[1,2,3],但也可以得到[2,4]或[1,5]。是否可以让函数返回多个子集?
找到了答案:)
我替换了代码
返回数字集;
通过
document.getElementById( outp)。value = document.getElementById( outp)。value + numberSet + \n;
非常感谢Cheeken
一个更多其他问题。如何格式化用于解析该功能的输入?以下代码似乎无效。 inp是输入所在的文本区域的ID(数字用分号分隔。变量ge有效,因此在那里没有问题(使用[1,2,3,4]对其进行了测试,并且可以正常工作。出了什么问题)
重新编辑:
找到了解决方案,需要将数组解析为浮点型
为(var i = 0; i< lines.length; i ++){
lines [i] = parseFloat(lines [i]);
}
findSum(document.getElementById( inp)。value.split(;) ,ge);
代码:
<!DOCTYPE html>
< html>
< head>
< script type = text / javascript>
函数powerset(arr){
var ps = [[]];
对于(var i = 0; i< arr.length; i ++){
对于(var j = 0 ,len = ps.length; j< len; j ++){
ps.push(ps [j] .concat(arr [i]));
}
}
return ps;
}
函数sum(arr){
var total = 0;
for(var i = 0; i< arr.length; i ++)
total + = arr [i];
return total
}
函数findSum(numbers,targetSum){
var numberSets = powerset(numbers);
for(var i = 0; i< numberSets.length; i ++){
var numberSet = numberSets [i];
if(sum(numberSet)== targetSum)
document.getElementById( outp)。value = document.getElementById( outp)。value + numberSet + \n;
}
}
函数main()
{
ge = document.getElementById( getal)。value;
findSum([1,1,0.5,0.1,0.2,0.2],ge);
}
< / script>
< / head>
< body>
<输入类型=按钮 onclick = main() value = tel /><输入类型=文本 id = getal />< br> ;
input< br>< textarea id = inp>< / textarea>< br
output< br>< textarea id = outp>< / textarea>< br>
document.getElementById( inp)。value.split(;)
< / body>
< / html>
更具体地说,您正在寻找特定金额$ a $ b的中的每一个集。
您可以使用以下代码完成此操作。
function powerset(arr){
var ps = [[]];
for(var i = 0; i< arr.length; i ++){
for(var j = 0,len = ps.length; j< len; j ++){
ps.push(ps [j] .concat(arr [i]));
}
}
return ps;
}
函数sum(arr){
var total = 0;
for(var i = 0; i< arr.length; i ++)
total + = arr [i];
return total
}
函数findSum(numbers,targetSum){
var numberSets = powerset(numbers);
for(var i = 0; i< numberSets.length; i ++){
var numberSet = numberSets [i];
if(sum(numberSet)== targetSum)
返回numberSet;
}
}
示例调用:
> findSum([1,2,3,4,5],6)
[1、2、3]
> findSum([1,2,3,4,5],0)
[]
> findSum([1,2,3,4,5],11)
[1、2、3、5]
如果您想收集所有总和为值的子集(而不是第一个,如上所述),则可以使用以下方法。
函数findSums(numbers,targetSum){
var sumSets = [];
var numberSets = powerset(数字);
for(var i = 0; i< numberSets.length; i ++){
var numberSet = numberSets [i];
if(sum(numberSet)== targetSum)
sumSets.push(numberSet);
}
返回sumSets;
}
示例调用:
>> findSums([1,2,3,4,5],5);
[[2,3],[1,4],[5]]
>> findSums([1,2,3,4,5],0);
[[]]
I am trying to write a program in javascript that gets an unspecified number of numbers out of a html textarea and tries all combinations (adding all numbers with eachother) to see if it mathches a number you specified.
Now I can make an array out of the string in the textarea and using for
loops I add these up (see below code). The problem how can you do this for an unspecified number of numbers that are to be added up (e.g. adding up 7 different number if you enter 7 numbers in textarea)? I was thinking of using a second array and, which gets the numbers to add up out of the first loop. And then make te lenght of the loop variable by using a for loop with the lenght of the array containing all numbers (lines in my example) as endvalue.
How can I fill in the values of this 2nd array, making sure all combinations are used?
By the way, I wanted this code because I am a auditor. Sometimes a client reverses a couple of amounts in one booking, without any comment. This code will make it a lot easier to check what bookings have been reversed
edit: The awnser of cheeken seems to be working I only have one remark. What if multiple sub sets of your power set added up result in the number you are looking for? e.g.:findSum([1,2,3,4,5],6) can result [1,2,3] but also [2,4] or [1,5]. is it possible to let the function return multiple sub sets?
Found the answer my self :)I replaced code
return numberSet;
By
document.getElementById("outp").value=document.getElementById("outp").value+ numberSet +"\n";
Thank you very much Cheeken
One more additional question. How do i format the input for parsing that function? The code below doesn't seem to work. inp is the ID of the textarea where the input is (the numbers are seperated with a semicolumn. The variable ge works so there is no problem there (tested it with [1,2,3,4] and it worked. What is wrong with this code?
re edit:
found the solution. The array needed to be parsed as a floating number added this code.`
for (var i=0; i < lines.length; i++) {
lines[i]= parseFloat(lines[i]);
}
findSum(document.getElementById("inp").value.split(";"), ge);
Code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function powerset(arr) {
var ps = [[]];
for (var i=0; i < arr.length; i++) {
for (var j = 0, len = ps.length; j < len; j++) {
ps.push(ps[j].concat(arr[i]));
}
}
return ps;
}
function sum(arr) {
var total = 0;
for (var i = 0; i < arr.length; i++)
total += arr[i];
return total
}
function findSum(numbers, targetSum) {
var numberSets = powerset(numbers);
for (var i=0; i < numberSets.length; i++) {
var numberSet = numberSets[i];
if (sum(numberSet) == targetSum)
document.getElementById("outp").value=document.getElementById("outp").value+ numberSet +"\n";
}
}
function main()
{
ge= document.getElementById("getal").value;
findSum([1,1,0.5,0.1,0.2,0.2], ge);
}
</script>
</head>
<body>
<input type="button" onclick="main()" value="tel" /><input type="text" id="getal" /><br>
input<br><textarea id="inp" ></textarea><br>
output<br><textarea id="outp" ></textarea><br>
document.getElementById("inp").value.split(";")
</body>
</html>
More concretely, you're looking for a particular sum of each set in the power set of your collection of numbers.
You can accomplish this with the following bit of code.
function powerset(arr) {
var ps = [[]];
for (var i=0; i < arr.length; i++) {
for (var j = 0, len = ps.length; j < len; j++) {
ps.push(ps[j].concat(arr[i]));
}
}
return ps;
}
function sum(arr) {
var total = 0;
for (var i = 0; i < arr.length; i++)
total += arr[i];
return total
}
function findSum(numbers, targetSum) {
var numberSets = powerset(numbers);
for (var i=0; i < numberSets.length; i++) {
var numberSet = numberSets[i];
if (sum(numberSet) == targetSum)
return numberSet;
}
}
Example invocation:
>> findSum([1,2,3,4,5],6)
[1, 2, 3]
>> findSum([1,2,3,4,5],0)
[]
>> findSum([1,2,3,4,5],11)
[1, 2, 3, 5]
If you'd like to collect all of the subsets whose sum is the value (rather than the first one, as implemented above) you can use the following method.
function findSums(numbers, targetSum) {
var sumSets = [];
var numberSets = powerset(numbers);
for (var i=0; i < numberSets.length; i++) {
var numberSet = numberSets[i];
if (sum(numberSet) == targetSum)
sumSets.push(numberSet);
}
return sumSets;
}
Example invocation:
>> findSums([1,2,3,4,5],5);
[[2,3],[1,4],[5]]
>> findSums([1,2,3,4,5],0);
[[]]
这篇关于将数组中所有数字组合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!