Hola,
este ejemplo esta completo y funcional, contiene dos listas con combinaciones:
Código Delphi
[-]
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
ListBox2: TListBox;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
list:TStringList;
function GetComb(listBox:TListBox):String;
procedure buscarComunes(const Cad1:Int64);
function extraerComunes(const res:int64):string;
procedure CompararCombinaciones(const Cad1, cad2:Int64);
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
list:=TStringList.create;
list.delimiter:=' ';
With listbox1, items do begin
add('10 12 21 33 45');
add('10 13 23 34 45');
add('16 33 14 27 10'); end;
With listbox2, items do begin
add('10 11 20 33 45');
add('2 20 22 34 44');
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
list.free; end;
function ValorCombinacion(list:TStrings):Int64;
var
elemento:Byte;
i: Integer;
begin
result := 0;
with list do
for i := 0 to Count - 1 do begin
elemento := StrToInt(strings[i])-1;
result := result or (Int64(1) shl elemento);
end;
end;
function TForm1.GetComb(listBox:TListBox):String;
begin
result:=listBox.items[listBox.itemIndex];
end;
function TForm1.extraerComunes(const res:int64):string;
var
i:integer;
begin
result:='';
for i :=0 to 49 do
if ((res and (int64(1) shl i))<>0) then
result:=result+IntToStr(i+1)+' ';
end;
procedure TForm1.CompararCombinaciones(const Cad1, cad2:Int64);
var
res:Int64;
comunes:string;
begin
res:=cad1 and cad2;
if (cad1 = cad2) then begin
end
else if (res <> 0) then begin
comunes:=extraerComunes(res);
Memo1.lines.add(GetComb(listbox1)+#9+GetComb(listbox2)+#9+'->'+#9+comunes);
end
else begin
end;
end;
procedure TForm1.buscarComunes(const Cad1:Int64);
var
cad2:Int64;
j:integer;
begin
with listbox2 do
for j := 0 to Count -1 do
begin
ItemIndex := j;
list.delimitedText:=Items[j];
cad2 := ValorCombinacion(list);
CompararCombinaciones(cad1, cad2);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
cad1:Int64;
i:integer;
begin
with ListBox1 do
for i := 0 to Count -1 do
begin
ItemIndex := i;
list.delimitedText:=Items[i];
cad1:= ValorCombinacion(list);
buscarComunes(cad1);
end;
end;
end.