program ShennonFano; uses crt; const a :array[1..6] of char = ('a','b','c','d','e','f'); { } af:array[1..6] of integer = (10, 8, 6, 5, 4, 3); { } { } procedure SearchTree(branch:char; full_branch:string; start_pos:integer; end_pos:integer); var dS:real; { } i, m, S:integer; { m - , S - , } c_branch:string; { } begin { } if (a<>' ') then c_branch := full_branch + branch else c_branch := ''; { : , } if (start_pos = end_pos) then begin WriteLn(a[start_pos], ' = ', c_branch); exit; end; { } dS := 0; for i:=start_pos to end_pos do dS:= dS + af[i]; dS := dS/2; { for, while, repeat } S := 0; i := start_pos; m := i; while ((S+af[i]<dS) and (i<end_pos)) do begin S := S + af[i]; inc(i); inc(m); end; { } SearchTree('1', c_branch, start_pos, m); { } SearchTree('0', c_branch, m+1, end_pos); end; begin WriteLn('Press <enter> to show'); ReadLn; ClrScr; { , } SearchTree(' ',' ', 1, 6); ReadLn; end;
program ShennonFano; uses crt; const a :array[1..6] of char = ('a','b','c','d','e','f'); { } af:array[1..6] of integer = (10, 8, 6, 5, 4, 3); { } { } procedure SearchTree(branch:char; full_branch:string; start_pos:integer; end_pos:integer); var dS:real; { } i, m, S:integer; { m - , S - , } c_branch:string; { } begin { } if (a<>' ') then c_branch := full_branch + branch else c_branch := ''; { : , } if (start_pos = end_pos) then begin WriteLn(a[start_pos], ' = ', c_branch); exit; end; { } dS := 0; for i:=start_pos to end_pos do dS:= dS + af[i]; dS := dS/2; { for, while, repeat } S := 0; i := start_pos; m := i; while ((S+af[i]<dS) and (i<end_pos)) do begin S := S + af[i]; inc(i); inc(m); end; { } SearchTree('1', c_branch, start_pos, m); { } SearchTree('0', c_branch, m+1, end_pos); end; begin WriteLn('Press <enter> to show'); ReadLn; ClrScr; { , } SearchTree(' ',' ', 1, 6); ReadLn; end;
Source: https://habr.com/ru/post/137766/
All Articles