delphi 口袋中有红、黄、蓝、白、黑5种颜色的球若干,每次取3个球,编写程序求得到三种不同颜色

2025-03-10 23:53:00
推荐回答(1个)
回答1:

递归求组合:
const n=5;
s0='红黄兰白黑';
type
charray=array[1..n] of string[2];
boolarray=array[1..n] of boolean;
var
s,ss:string;
ch:charray;
t:boolarray;
i,m,num:integer;

procedure next(t:boolarray;ss:string;m:integer;var num:integer);
var
i:integer;
begin
if m=0 then begin writeln(ss); inc(num); end
else
for i:=1 to n do
if t[i] then begin
t[i]:=false;
next(t,ss+ch[i],m-1,num);
end;
end;

begin
s:=copy(s0,1,length(s0));
for i:=1 to n do begin ch[i]:=copy(s,2*i-1,2); t[i]:=true; end;
ss:='';
m:=3;
next(t,ss,m,num);
writeln;
writeln(num);
end.