假设有6个数字,分别为1 2 3 4 5 6 求四个数字为一组(不分顺序),有多少组不重复的组合?编程输出到一个MEMO上.
我用笔和纸推算一下
(1) 1 2 3 4
(2) 1 2 3 5
(3) 1 2 3 6
(4) 1 2 4 5
(5) 1 2 4 6
(7) 1 2 5 6
(8) 1 3 4 5
(9) 1 3 4 6
(10) 1 3 5 6
(11) 1 4 5 6
(12) 2 3 4 5
(13) 2 3 4 6
(14) 2 4 5 6
(15) 3 4 5 6
共15组?如何编码解答此题??
我用笔和纸推算一下
(1) 1 2 3 4
(2) 1 2 3 5
(3) 1 2 3 6
(4) 1 2 4 5
(5) 1 2 4 6
(7) 1 2 5 6
(8) 1 3 4 5
(9) 1 3 4 6
(10) 1 3 5 6
(11) 1 4 5 6
(12) 2 3 4 5
(13) 2 3 4 6
(14) 2 4 5 6
(15) 3 4 5 6
共15组?如何编码解答此题??
解决方案 »
- 我用Access数据库做的系统,为什么每过几天就会出现数据库被独占?
- 关于文本框不能用ctrl+c和ctrl+v的问题
- 在DBGrid中能不能对TQuery的多表查询结果进行修改?另外怎么样根据查询的结果设置DBGrid的列。
- 怎样按回车键转移焦点到下一控件?
- 程序调用shellexecute/winexe时触发的是什么消息?
- 请教,如何编码转为汉字???
- 一个极其简单的问题,寻求最简单的方法
- 寻找 能编辑 .PDF文件的控件(ActiveX 或 VCL), 类似于Windows的RichEdit。
- 憋了很久的问题
- 运行DELPHI5的理想配置是啥?
- 新手交流贴。给FireBird新手。Oracle可以从dual里Select 资料,FireBird呢?
- 新手交流贴,给firebird.FireBird的日期预定义字符的事情。
program Project1;{$APPTYPE CONSOLE}uses
SysUtils;var
n,t:longint;
a:array[1..8]of integer;
flag:array[1..8]of boolean;
procedure search(depth:integer);
var
i:integer;
begin
if (depth> 6) then
begin
for i:=1 to 4 do write(a[i]:4);
writeln;
inc(t);
exit;
end;
for i:=1 to 6 do
if flag[i]=false then
begin
a[depth]:=i;
flag[i]:=true;
search(depth+1);
flag[i]:=false;
end;
end;
begin
t:=0;
fillchar(flag,sizeof(flag),false);
search(1);
writeln('Total=',t);
read(n);
end.
最后还要处理一下,每行重新按大小排位(如6312排成1236)后,再消除重复项就行了
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
FBase, FCnt, FIndex : Integer;
FArr : array of Boolean;
FErr : string;
function CheckEdit: Int64;
procedure DoEnum(First, Depth: Integer);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
procedure OutputEnum;
var tm: Cardinal;
begin
Memo1.Lines.BeginUpdate;
tm := GetTickCount;
try
SetLength(FArr, FBase);
FIndex := 0;
DoEnum(0, FCnt);
finally
Caption := Format('%d ms', [GetTickCount-tm]);
Memo1.Lines.EndUpdate;
end;
end;
const IMax = 5000;
var nSum : Int64;
begin
nSum := CheckEdit;
if(nSum<0)then
begin
Memo1.Text := '[Error] '+FErr;
Exit;
end; Memo1.Text := Format('Total: %d', [nSum]);
if(nSum>IMax)then
begin
Memo1.Lines.Add('[Skipped] Too many...');
end else
OutputEnum;
end;function TForm1.CheckEdit: Int64;
const
SRefName : array[0..1]of string = ('Count', 'Base');
IOFCheck = High(UInt64)shr 16;
var
i, m : Integer;
strs : array[0..1]of PInteger;
edits : array[0..1]of TEdit;
begin
Result := -1;
strs[0] := @FCnt;
strs[1] := @FBase;
edits[0] := Edit1;
edits[1] := Edit2;
for i:=Low(strs) to High(strs) do
with edits[i] do
try
strs[i]^ := StrToInt(Text);
except
FErr := Format('%s is not an Integer: "%s"', [SRefName[i], Text]);
Exit;
end; if(FCnt<1)then
begin
FErr := Format('Count(%d) < 1', [FCnt]);
Exit;
end else
if(FBase<FCnt)then
begin
FErr := Format('Count(%d) > Base(%d)', [FCnt, FBase]);
Exit;
end; Result := 1;
m := FBase - FCnt;
if(m>FCnt)then
m := FCnt;
for i:=1 to m do
begin
if( Result > (High(Int64)div(FBase+1-i)) )then
begin
Result := -1;
FErr := 'Overflow!';
Exit;
end;
Result := Result * (FBase+1-i) div i;
end;
end;procedure TForm1.DoEnum(First, Depth: Integer);
procedure DoOutput(Range, Last: Integer);
var
i : Integer;
s : string;
begin
Inc(FIndex);
s := '';
for i:=0 to Range-1 do
if(FArr[i])then
s := Format('%s'#9'%d', [s, i+1]);
Memo1.Lines.Add(Format('%d.%s'#9'%d', [FIndex, s, Last]));
end;
var i: Integer;
begin
if(Depth>1)then
for i:=First to FBase-Depth do
begin
if(i>First)then
FArr[i-1] := False;
FArr[i] := True;
DoEnum(i+1, Depth-1);
end else
for i:=First+1 to FBase do
DoOutput(First, i);
end;end.