一个算法题: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.
- 一个算法题:10个队分成3组,问有多少种分法
- 把26个字母分成3组,有多少种分法
- 用对角线将凸n边行分成三角形,问有多少种分法
- php 把一个数组分成有n个元素的二维数组的算法
- hdu 2512 斯特灵数,贝尔数 将N个元素分成若干个集合,集合不为空,有多少种分法
- 一个环,有n个点, 问从0点出发,经过k步回到原点有多少种方法
- 根据游戏规则算出一个屋里有多少个桌子多少个人的算法
- 每日一道算法题:1024!末尾有多少个0?
- java 程序题 递归算法 判断一个整数化成二进制之后有多少个1 (两种思路)
- 一个楼梯有50个台阶,一次可以走一个台阶,也可以走两个台阶,也可以走三个台阶,问总共有多少种走法.
- 一个字符里有多少个1
- 一个数组有多少个属性
- 用函数和递归求n个三角形能把一个平面分成最多多少块
- N个相同的元素分成M组(允许某些组为空),有多少种不同的分法?
- 有N个硬币,每个硬币有无数个,问有多少方案
- 计算一个整数的二进制表示有多少个1(别人的最快算法)
- n对字母分成两组,共有多少种分法?
- 面试算法题:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
- [HDU 4118]Holiday's Accommodation[图论][非递归dfs]
- 华为上机题
- main.cpp first defined here 解决
- Linux高性能集群 - 资源管理和系统管理
- A*算法入门
- 一个算法题:10个队分成3组,问有多少种分法
- 排序算法小结
- paip. 'QObject::QObject(const QObject&)' is private问题的解决.
- paip.c++ static 变量的定义以及使用...
- 一个简单的内联union问题
- 新开论坛,大家一起建设!
- C/C++中自定义信息输出——printf与宏的配合使用
- 疯牛 nyist586(二分答案)
- 【LeetCode】Merge Sorted Array ---合并数组