分治算法练习(一)

分治算法_小车问题

AYYZOJ p1428

COGS p559

具体讲解见此课件

 1 //分治
 2 program car1(input,output);
 3 const zero=1e-4;
 4 var s,a,b,c,c0,c1,t1,t2,t3,t4:real;
 5 BEGIN
 6 assign(input,'car.in');
 7 assign(output,'car.out');
 8 reset(input);
 9 rewrite(output);
10   readln(s,a,b);
11   c0:=0;
12   c1:=s;
13   repeat
14     c:=(c0+c1)/2;
15     t3:=c/b;  //甲到K乘汽车用时;也是乙走到C用时。即 乙TAC=甲TAK
16     t1:=t3+(s-c)/a;  //甲到终点用时。即 甲TAB
17     t4:=(c-t3*a)/(a+b);  //乙从C走,车从K来接乙相遇用时。即 乙TCD
18     t2:=t3+t4+(s-(t3+t4)*a)/b;   //乙到终点用时。即 乙TAB
19     if t1<t2 then c1:=c else c0:=c;   //甲的时间短,走得快,则让汽车少载一段甲;乙的时间短,走得快,则让汽车多载一段甲
20   until abs(t1-t2)<zero;
21   writeln(t1:0:2);
22 close(input);
23 close(output)
24 end.
25 
26 ------------------------------------------------------------------
27 //数学
28 var
29   s,a,b,k,x,t:real;
30 begin
31 assign(input,'car.in');
32 reset(input);
33 assign(output,'car.out');
34 rewrite(output);
35   readln(s,a,b);
36    k:=s*(a+b)/(3*a+b);
37    t:=k/b+(s-k)/a;
38   writeln(t:0:2);
39 close(input);close(output);
40 end.
分治or数学

分治算法_取余运算

AYYZOJ p1429

COGS p1130

具体讲解在这个课件

 1 program p1429;
 2 var
 3  b,p,k,i,len,rest,temp:longint;
 4  binary:array[1..32] of longint;
 5 begin
 6  readln(b,p,k);
 7  len:=0;
 8  temp:=p;
 9  while temp<>0 do
10   begin
11    len:=len+1;
12    binary[len]:=temp mod 2;
13    temp:=temp div 2;
14   end;
15   rest:=1;
16   for i:=len downto 1 do
17    begin
18     temp:=rest*rest mod k;
19     if binary[i]=1 then rest:=b mod k*temp mod k
20                    else rest:=temp;
21    end;
22   writeln(b,'^',p,' mod ',k,'=',rest);
23 end.
我的程序
 1 Program cogs1130;
 2   Var
 3     B,P,K,ANS:Longint;
 4   Begin
 5     Assign(Input,'dmod.in');
 6     Assign(Output,'dmod.out');
 7     Reset(Input);
 8     Rewrite(Output);
 9     Readln(B,P,K);
10     ANS:=1;
11     B:=B Mod K;
12     While P<>0 Do
13       Begin
14         If P And 1=1  {p mod 2=1} Then ANS:=ANS*B Mod K;
15         P:=P Shr 1;   {p:=p div 2}{p:=p>>1}
16         B:=B*B Mod K;
17       End;
18     Writeln(ANS);
19     Close(Input);
20     Close(Output);
21   End.
另一种写法

分治算法_成绩排序

AYYZOJ p1431

 1 program p1431;
 2 Var a,b:array[1..10000] of longint;
 3   i,j,t,n:longint;flag:boolean;
 4 Begin
 5  readln(n);
 6   for i:=1 to N do Readln(b[i],a[i]);
 7   for j:=1 to N-1 do begin
 8     flag:=true;
 9     for i:=1 to N-j do
10       if a[i]>a[i+1] then begin
11         t:=a[i];a[i]:=a[i+1];a[i+1]:=t;
12         t:=b[i];b[i]:=b[i+1];b[i+1]:=t;
13         flag:=false;
14       end;
15     if flag then break;
16   end;
17   for i:=1 to N do writeln(b[i],' ',a[i]);
18 end.
我的程序

 

分治算法_循环比赛

AYYZOJ p1433

这个课件讲得很清楚了

 1 program p1433;
 2 var
 3  i,j,h,m,n:integer;
 4  a:array[1..100,1..100] of integer;
 5 begin
 6  readln(m);
 7  n:=1; a[1,1]:=1; h:=1;
 8  for i:=1 to m do n:=n*2;
 9   repeat
10    for i:=1 to h do
11     for j:=1 to h do
12     begin
13      a[i,j+h]:=a[i,j]+h;
14      a[i+h,j]:=a[i,j+h];
15      a[i+h,j+h]:=a[i,j];
16     end;
17     h:=h*2;
18    until h=n;
19    for i:=1 to n do
20     begin
21      for j:=1 to n do write(a[i,j]:4); writeln;
22     end;
23 end.
参考程序

分治算法_棋子移动

AYYZOJ p1435

COGS p1161

这个课件讲得很清楚了

 1 const max=100;
 2 var n,st,sp:integer;
 3     c:array[1..max] of char;  {工作场所}
 4 
 5 procedure print;  {打印}
 6   var i:integer;
 7   begin
 8     write('step',st:2,':');
 9     for i:=1 to 2*n+2 do write(c[i]);
10     writeln;
11     st:=st+1
12   end;
13 
14 procedure init(n:integer);  {初始化}
15   var i:integer;
16   begin
17     st:=0;
18     sp:=2*n+1;
19     for i:=1 to n do c[i]:='o';
20     for i:=n+1 to 2*n do c[i]:='*';
21     c[2*n+1]:='-';c[2*n+2]:='-';
22     print
23   end;
24 
25 procedure move(k:integer);  {移动一步}//将两个横线(空)移到k处(以左横线位置为序号)
26   var j:integer;
27   begin
28     for j:=0 to 1 do begin c[sp+j]:=c[k+j];c[k+j]:='-';end;
29     sp:=k;
30     print
31   end;
32 
33 procedure mv(n:integer);    {主要过程}
34   var i,k:integer;
35   begin
36     if n=4 then begin
37                   move(4);
38                   move(8);
39                   move(2);
40                   move(7);
41                   move(1)
42                 end
43            else begin
44                   move(n);
45                   move(2*n-1);
46                   mv(n-1)
47                 end
48   end;
49 
50 begin  {main}
51   assign(input,'chessman.in');
52   assign(output,'chessman.out');
53   reset(input);
54   rewrite(output);
55   readln(n);
56   init(n);
57   mv(n);
58   close(input);
59   close(output)
60 end.
参考程序
posted @ 2016-02-05 11:54  ZJQCation  阅读(562)  评论(0编辑  收藏  举报