一个算法题:10个队分成3组,问有多少种分法

来源:互联网 发布:广州谷得知乎 编辑:程序博客网 时间:2024/05/17 12:55

前面的尽量多分,例如5个对分成3组,则为前面两队每队2人,后面一队1人,

现在有10队,分成3组,则前面2队每队4人,后面一队2人,问有所少种分法

刚开始一看挺简单的,其实挺复杂的,要用到高中的排列组合,根据排列组合先画出一个大致的图:

unit uGroup;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  Dialogs,Contnrs, StdCtrls;type  TForm3 = class(TForm)    生成: TButton;    ListBox1: TListBox;    Label1: TLabel;  type  MyArry=array of Integer;  function GetUnitCount():Integer;    procedure 生成Click(Sender: TObject);    function GetRemain(ary:MyArry):string;    function GetNumInAllCondition():TStringList;  private    { Private declarations }  public    { Public declarations }    UnitCount:Integer;  //每组的数量    LastCount:Integer;  //最后一组的数量  end;var  Form3: TForm3;const  MainCount:Integer=10;    //总数  GroupCount:Integer=3;   //组数implementation{$R *.dfm}{ TForm3 }procedure TForm3.生成Click(Sender: TObject);var i:Integer;o:TStringList;begin  o:=GetNumInAllCondition;  ListBox1.Clear;  for I := 0 to o.Count - 1 do  begin    ListBox1.Items.Add(o.Strings[i]);  end;  ShowMessage('一共有'+IntToStr(o.Count)+'种组合情况!');  o.Free;end;function TForm3.GetNumInAllCondition: TStringList; //获取所有情况下的组合var o:TStringList;i,j,a,b,c,d,f,g,h:Integer;str:string;ary:MyArry;begin  o:=TStringList.Create;  //获取每组个数以及最后一组的个数  GetUnitCount;  //获取第一组的所有情况  for I := 1 to MainCount do  begin    for j := i+1 to MainCount do    begin      for a := j+1 to MainCount do      begin        for b := a+1 to MainCount do        begin          //生成第二组数          for c := 1 to MainCount do          begin              for d := c+1 to MainCount  do            begin              for f := d+1 to MainCount do              begin                for g := f+1 to MainCount do                begin                  if (c<>i) and (c<>j) and (c<>a) and (c<>b) and (d<>i) and (d<>j) and (d<>a) and (d<>b) and (f<>i) and (f<>j) and (f<>a) and (f<>b) and (g<>i) and (g<>j) and (g<>a) and (g<>b) then                  begin                     str:='';                     str:='['+inttostr(i)+','+inttostr(j)+','+inttostr(a)+','+inttostr(b)+']['+inttostr(c)+','+inttostr(d)+','+inttostr(f)+','+inttostr(g)+']';                     SetLength(ary,8);                     ary[0]:=i;                     ary[1]:=j;                     ary[2]:=a;                     ary[3]:=b;                     ary[4]:=c;                     ary[5]:=d;                     ary[6]:=f;                     ary[7]:=g;                     str:=str+'['+GetRemain(ary)+']';                     o.Add(str);                     h:=o.Count;                  end;                end;              end;            end;          end;        end;      end;    end;  end;  Result:=o;end;function TForm3.GetRemain(ary: MyArry): string;  //取最后一组数var i,j:Integer;isequal:Boolean;str:string;begin  for I := 1 to MainCount do  begin    isequal:=False;    for j := 0 to Length(ary)-1 do    begin       if(i=ary[j])then       begin         isequal:=True;       end;    end;    if(not isequal) then      str:=str+inttostr(i)+',';  end;  str:=Copy(str,1,Length(str)-1);  Result:=str;end;function TForm3.GetUnitCount: Integer;   //求每组的数量var x:Integer;y,z:Integer;begin  z:=MainCount;  x:=0;  y:=0;  while True do  begin     x:=x+1;     y:=y+1;     y:= GroupCount*x-maincount;     if(y>0)then     begin       Break;     end;  end;  LastCount:=MainCount-x*(groupcount-1);  UnitCount:=x;  Result:=x;end;end.


 

 

 

原创粉丝点击