Algorithm for finding all possible combinations of integer numbers which their sum equals the target.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I want to develop an algorithm which finds all the possible combinations to fill an area between 5 m^2 and 50 m^2 (integer), with blocks of size 1-10 m^2 (integer).
The rules are:
1.for every area we have option of choosing only 2 different blocks, one big one small.
2.if the big fills exactly the area we use one.
3.we are not allowed to use a number of smalls which their sum is >= big.
In order to understand better imagine that you want to put tiles in your kitchen, and you want the number and the type of each tile.
For ex. for 7 the combinations are: (7)(6,1)(5,2)(5,1,1) (4,3)(4,1,1,1) (3,2,2) (3,1,1,1,1:this cant be used because you can put 3,3,1 instead.) (2,5: this you cant because you already chose 5,2) (1,3,3) (1,2,2,2).
Below I have my code which I do not know how to finalize.
Any help is much appreciated.
target = 7 ;
for i=10:-1:1
if (i<=target)
if (i==target)
disp(['(',num2str(i),')'])
end
for j=1:1:10
if ((i+j)==target)
if (i>j) % in order to exclude double entries
disp(['(',num2str(i),',',num2str(j),')'])
% a = j/i;
% for l=1:1:a
% disp(['(',num2str(i),',',num2str(a),')'])
% end
else
for l=(j-1):-1:1
end
end
end
end
end
end
3 Kommentare
John D'Errico
am 14 Jul. 2014
Complex rules ensure that no simple solution exists. However, a programmatic scheme will work, as long as you are careful.
Antworten (0)
Siehe auch
Kategorien
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!