排列

program pailie;
const
maxm=1000;
var
n,m,sum:longint;
a:array[1..maxm]of longint;

procedure print;
var
i:longint;
begin
for i:=1 to m do write(a[i]);
writeln;
end;


procedure play(k:longint);
var
i:longint;
begin
if k=m+1 then begin
inc(sum);
print;
exit;
end;

for i:=1 to n do
begin
a[k]:=i;
play(k+1);
end;

end;

begin
assign(input,'win.in');
assign(output,'win.out');
reset(input);
rewrite(output);

readln(n,m);
sum:=0;

play(1);
writeln('the total is:',sum);

close(input);
close(output);

end.

从n个自然数中取出m个数的排列(允许重复);

 
program pai2;
const
 maxm=100;
var
 i,sum,n,m:longint;
 a:array[1..maxm]of integer;
 boo:array[1..maxm]of boolean;

procedure print;
var
 i:longint;
begin
    for i:=1 to m  do write(a[i]);
    writeln;
end;

procedure play(k:longint);
var
   i:longint;
begin
     if k=m+1 then begin
         inc(sum);
         print;
         exit;
     end;

     for i:=1 to n do
      if  boo[i] then
       begin
            a[k]:=i;
            boo[i]:=false;
            play(k+1);
       end;

end;

begin
    assign(input,'win.in');
    assign(output,'win.out');
    reset(input);
    rewrite(output);

    //init
    readln(n,m);
    sum:=0;
    fillchar(boo,sizeof(boo),1);
    fillchar(a,sizeof(a),0);

    play(1);
    writeln('the total is:',sum);

    close(input);
    close(output);
end.

  

   n个自然数中取出m个数的排列(不允许重复(n>=m);

以下限定次数,若干限定次数,即每个数都有

View Code
 1 program pai3;
2 const
3 maxt=100;
4 var
5 i,sum,n,m:longint;
6 time:array[1..maxt]of longint;
7 a:array[1..maxt]of integer;
8 flag:boolean;
9
10 procedure print;
11 var
12 i:longint;
13 begin
14 for i:=1 to m do write(a[i]);
15 writeln;
16 end;
17
18
19 procedure play(k:longint);
20 var
21 i:longint;
22 begin
23 if flag then
24 begin
25 if k=2 then flag:=true;
26 exit;
27 end;
28
29 if k=m+1 then
30 begin
31 print;
32 flag:=true;
33 exit;
34 end;
35
36 for i:=1 to n do
37 if time[i]>0 then begin
38 time[i]:=time[i]-1;
39 a[k]:=i;
40 play(k+1);
41 end;
42
43 end;
44
45
46 begin
47 assign(input,'win.in');
48 assign(output,'win.out');
49 reset(input);
50 rewrite(output);
51
52 readln(n,m);
53 flag:=false;
54 for i:=1 to n do time[i]:=1;
55
56
57 play(1);
58
59
60 writeln(sum);
61 close(input);
62 close(output);
63
64
65 end.


限定次数

转载于:https://www.cnblogs.com/llwoll/archive/2012/03/24/2415922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值