主题:在N人中选M人(汗!发了2个..看这个吧)
在N人中选M人(m<=n)
样例输入:n,m=5 3
样例输出:
5 4 3
5 4 2
5 4 1
5 3 2
5 3 1
5 2 1
4 3 2
4 3 1
4 2 1
3 2 1
c(5,3)=10
我做了一个,但...帮我大改改吧...
const maxn=15;
var n,m,i,j,k,l:integer;
a,b:array[1..15] of integer;
f:boolean;
begin
read(n,m);
i:=1;
j:=n;
k:=m;
repeat
b[i]:=j;
a[i]:=k;
j:=j-1;
k:=k-1;
inc(i);
until i=m+1;
i:=m;
repeat
for j:=1 to m do write(b[j],' ');
writeln;
b[i]:=b[i]-1;
k:=0;
if b[i]<a[i] then begin
while b[i-k]<a[i-k] do begin
inc(k);
b[i-k]:=b[i-k]-1;
end;
j:=0;
l:=0;
repeat
b[m-k+1]:=b[m-k-1]-1;
inc(l)
until l=k;
end;
until b[i]=m;
end.
(两种..都不对...)
for j:=m downto 1 do
if b[j]<a[j] then begin
k:=j-1;
repeat
b[k]:=b[k]-1;
if not b[k]-1<a[k-1] then begin
b[j]:=b[j+1]-1;
end else begin
inc(k);
end;
if (b[m]<a[m]) then begin
k:=k-1;
j:=j-1;
f:=false;
end else break;
until f;
end;
until b[1]<a[1];
dec(i);
until b[i]=m;
end.
我是想,从N开始,n,n-1,n-2...有M个,
在把最后一个-1,-到了1就倒数第二个-1,最后一个就比倒数第二个少1,然后倒数第二个-到了2,就把倒数第3个 又退位...依次类推.(上程序a树组为退位的1,2,3..B为每钟排列)
样例输入:n,m=5 3
样例输出:
5 4 3
5 4 2
5 4 1
5 3 2
5 3 1
5 2 1
4 3 2
4 3 1
4 2 1
3 2 1
c(5,3)=10
我做了一个,但...帮我大改改吧...
const maxn=15;
var n,m,i,j,k,l:integer;
a,b:array[1..15] of integer;
f:boolean;
begin
read(n,m);
i:=1;
j:=n;
k:=m;
repeat
b[i]:=j;
a[i]:=k;
j:=j-1;
k:=k-1;
inc(i);
until i=m+1;
i:=m;
repeat
for j:=1 to m do write(b[j],' ');
writeln;
b[i]:=b[i]-1;
k:=0;
if b[i]<a[i] then begin
while b[i-k]<a[i-k] do begin
inc(k);
b[i-k]:=b[i-k]-1;
end;
j:=0;
l:=0;
repeat
b[m-k+1]:=b[m-k-1]-1;
inc(l)
until l=k;
end;
until b[i]=m;
end.
(两种..都不对...)
for j:=m downto 1 do
if b[j]<a[j] then begin
k:=j-1;
repeat
b[k]:=b[k]-1;
if not b[k]-1<a[k-1] then begin
b[j]:=b[j+1]-1;
end else begin
inc(k);
end;
if (b[m]<a[m]) then begin
k:=k-1;
j:=j-1;
f:=false;
end else break;
until f;
end;
until b[1]<a[1];
dec(i);
until b[i]=m;
end.
我是想,从N开始,n,n-1,n-2...有M个,
在把最后一个-1,-到了1就倒数第二个-1,最后一个就比倒数第二个少1,然后倒数第二个-到了2,就把倒数第3个 又退位...依次类推.(上程序a树组为退位的1,2,3..B为每钟排列)