从M个数取N个,和为固定值

发布时间 2023-07-27 15:49:48作者: 涂磊
从数组中取出任意个数,求和为指定值的解
var
  TestArr:TArray<integer>;
procedure CalSum(Arr:TArray<Integer>;Res:Integer);
var
 i,j,sum:Integer;
 temp:string;
begin
  for i:= 1 to  (1 shl Length(Arr))-1 do
  begin
    sum:=0;
    temp:='';
    for j:= 0 to Length(Arr)-1 do
    begin
      if (i and (1 shl j))<>0 then
      begin
        sum:=sum+Arr[j];
        temp:=temp+Arr[j].ToString;
      end;
    end;
    if sum=Res then
    begin
      Writeln(temp);
    end;
  end;
end;
begin
  TestArr:=[1,2,3,4,5,6,7,8,9];
  CalSum(TestArr,10);
  Readln;
end.

 Excel解决方案

Excel规划求解求哪几个数字之和等于一个固定值