请教一个组合算法,谢谢!

呵呵呵呵1sss 2012-10-29 07:10:19
我有一组数据,两个列:【列1】,【列2】
其中【列1】的值是唯一的;【列2】的值都是小于1至49的数字

这组数据大概有40条左右
我想求个算法,将所有【列2】值相加后的值为50的组合都列出来,不知道可不可行,计算量是不是非常巨大???

特别说明:如果出现特殊情况,比如列2的值都大于25了,就拆分。

请大家给个思路,当然有delphi的算法代码做为测试更好,谢谢!

如果分不够,可以说一声,我加就是了。
...全文
239 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
广州佬 2012-11-04
  • 打赏
  • 举报
回复
59行的注释应为://将上之前的数与当前数(和少于50)继续匹配下一个数
广州佬 2012-11-04
  • 打赏
  • 举报
回复
新建工程、分别拖Tmemo和TButton各一个到窗体、点击Button1后,将下列代码覆盖你的unit1:
nit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
type
  TComputingData = record//存放数据
    ID : string;//id编号
    v : integer;//值
    select : boolean;//是否被组合了
  end;
  TUpshotData = record//存放结果
    ID:string;
    v:string;
  end;

var tmp:array[0..49]of TComputingData;//存放数据

procedure TForm1.Button1Click(Sender: TObject);
var i,len:integer;
    upshot:array of TUpshotData;//存放结果
    bID,bV:string;
  function GetMatch(var idstr,vstr:string; v,index:integer):boolean;
  var j:integer;
      aID,aV:string;
  begin
    Result := false;//先假定没匹配
    for j:=index to 49 do begin
      if tmp[j].select then continue;
      if v+tmp[j].v = 50 then begin //匹配上了
        tmp[j].select:=true;
        idstr:=idstr+','+Format('%.2d',[j]);
        vstr:=vstr+','+inttostr(tmp[j].v);
        Result := true;
        exit;//不再找
      end;
    end;
    //未匹配:
    for j:=index to 49 do begin   //将上一个数与当前数(和少于50)继续匹配下一个数
      if tmp[j].select then continue;
      if v+tmp[j].v>50 then continue;
      aID:=idstr+','+Format('%.2d',[j]);
      aV:=vstr+','+inttostr(tmp[j].v);
      tmp[j].select:=true;
      if GetMatch(aID,aV,v+tmp[j].v,j+1) then begin//匹配上了
        idstr:=aID;
        vstr:=aV;
        Result := true;
        break;//不再找
      end
      else
        tmp[j].select:=false;
    end;
  end;
begin
  memo1.Text:='';
  randomize;
  for i:=0 to 49 do begin
    tmp[i].ID:=Format('%.2d',[i]);
    tmp[i].v:=random(49)+1;
    tmp[i].select:=false;
  end;
  memo1.Lines.Append('待处理数据:');
  for i:=0 to 49 do
    memo1.Lines.Append(Format('%.2d',[i])+'----'+inttostr(tmp[i].v));
  len:=-1;
  for i:=0 to 48 do begin
    if tmp[i].select then continue;
    bID:=Format('%.2d',[i]);
    bV:=inttostr(tmp[i].v);
    tmp[i].select:=true;
    if GetMatch(bID,bV,tmp[i].v,i+1) then begin
      inc(len);
      setlength(upshot,len+1);
      upshot[len].ID:=bID;
      upshot[len].v:=bV;
    end
    else
      tmp[i].select:=false;
  end;
  if len<0 then
    memo1.Lines.Append('没有能组合为50的结果。')
  else begin
    memo1.Lines.Append('找到的组合结果是:');
    for i:=0 to len do
      memo1.Lines.Append('ID号:'+upshot[i].ID+'---对应的值:'+upshot[i].v);
  end;
end;

end.
hsfzxjy 2012-11-03
  • 打赏
  • 举报
回复
如果有3列或以上的数据加起来等于50的话怎么算?
覃Fg 2012-11-03
  • 打赏
  • 举报
回复
神仙,1+1能加出个50来? 数组根据你的数据初始化了没有?
呵呵呵呵1sss 2012-10-30
  • 打赏
  • 举报
回复
谢谢qinkydep!

程序运行后,没有任何结果!
覃Fg 2012-10-30
  • 打赏
  • 举报
回复
//初始化数组的值
for i := 0 to arrayLength do

要改为
//初始化数组的值
for i := 0 to arrayLength-1 do
覃Fg 2012-10-30
  • 打赏
  • 举报
回复
var
intsA: array of Integer; //保存列2
strsA: array of string; //保存列1
sList: TStringList;
arrayLength: Integer;
i, j: Integer;
begin
arrayLength := 50; //列的长度
SetLength(intsA, arrayLength); //初始化数组
SetLength(strsA, arrayLength); //初始化数组

//初始化数组的值
for i := 0 to arrayLength do
begin
intsA[i] := 1;
strsA[i] := 'aaaa';
end;
sList := TStringList.Create;
try
for i := 0 to arrayLength - 2 do
begin
for j := 1 to arrayLength - 1 do
begin
if intsA[i] + intsA[j] = 50 then //如果为50,保存列1的值
sList.Add(strsA[i] + ' | ' + strsA[j])
end;
end;

//看一下有哪些值保存了
for i := 0 to sList.Count - 1 do
ShowMessage(sList[i]);
finally
sList.Free;
end;

end;
覃Fg 2012-10-30
  • 打赏
  • 举报
回复
你是做开发的?
呵呵呵呵1sss 2012-10-30
  • 打赏
  • 举报
回复
这样,简化下题目,就找出哪些组合等于50的就行了。但要标记出组合的【列1】的唯一编号。
谢谢!
hsfzxjy 2012-10-30
  • 打赏
  • 举报
回复
就拆分?什么意思?
不过这个算法倒是挺像回溯的。。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧