247
Електронна презентація вивчення курсу дисципліни “Алгоритмізація та програмування” Розробник: Яцків Любов Федорівна, викладач вищої категорії. 2011

Алгоритмізація та програмування

  • Upload
    sclnau

  • View
    54

  • Download
    4

Embed Size (px)

DESCRIPTION

Електронна презентація вивчення курсу дисципліни “Алгоритмізація та програмування”

Citation preview

  • : , .

    2011

  • 1.

  • : (): ( ): : : : - .

  • , - , . ? ?

  • - ( ) () () , , : , , : , , : , : JavaScript, Java, Perl, PHP, ASP

  • 1970 () -

    (, , )

  • ?program ;const ;{}var ; {}

    begin { }end.{ }

  • ? , . , ( ). , ( ). , ( , sin).

  • , , (A-Z)

    _ , +, =, !, ? . ? AXby R&B 4Wheel PesBarbos TU154 [QuQu] _ABBA A+B

  • const i2 = 45; { }pi = 3.14; { }

    qq = ''; { }

    L = True; { } ! : True (, ") False (, "")

  • , , . . :integer{ }real{ }char{ }string{ }boolean { } ( ):var a, b: integer;Q: real;s1, s2: string;

  • ? . .:program qq;var a, b: integer;begin a := 5; b := a + 2; a := (a + 2)*(b 3);end.a?55b?5+27a57*428

  • : := ; : + - * / div mod

    ( )

  • program qq;var a, b: integer; x, y: real; begin a := 5; 10 := x; y := 7,8; b := 2.5; x := 2*(a + y); a := b + x;end. ? :=

  • program qq;var a, b: integer;begin a := 5; b := a + 2; a := (a + 2)*(b 3); b := a div 5; a := a mod b; a := a + 1; b := (a + 14) mod 7;end.

    ab??

    57285344

  • , , div, mod 2 3 5 4 1 7 8 6 9z := (5*a*c+3*(c-d))/a*(b-c)/ b; 2 6 3 4 7 5 1 12 8 11 10 9x:=(a*a+5*c*c-d*(a+b))/((c+d)*(d-2*a));

  • . . :program qq;var a, b, c: integer;begin read ( a, b ); c := a + b; writeln ( c );end.

  • read ( a ); { a}read ( a, b ); { a b} ? :25 30 Enter:25 30

  • write ( a ); { a}writeln ( a ); { a }writeln ( !' ); { }writeln ( : ', c ); { c}writeln ( a, '+', b, '=', c );

  • program qq;var i: integer; x: real;begin i := 15; writeln ( '>', i, '', i:5, '', x, '', x:10, '', x:7:2, '
  • program qq;var a, b, c: integer;begin writeln( '); read ( a, b ); c := a + b; writeln ( a, '+', b, '=', c );end.: 25 30 25+30=55 '

  • - c := a + b; a, b c

  • "4": , . : : 4 5 7 4+5+7=16 4*5*7=140"5": , , . : : 4 5 7 4+5+7=16 4*5*7=140 (4+5+7)/3=5.33

  • 2.

  • . . : , , , .: ( )., , .

  • 1. -

  • 1. program qq;var a, b, max: integer;begin writeln( '); read ( a, b ); if a > b then begin end else begin end; writeln ( ', max);end.max := a;max := b;

  • if then begin { , } end else begin { , } end;: else (else ) ( ) , begin end

  • ?if a > b then begin a := b; end else b := a; end;if a > b then begin a := b; else begin b := a; end;if a > b then begin a := b; end; else begin b := a; end;if a > b then begin a := b; end else b > a begin b := a; end;beginendbeginend

  • 2. -

  • 2. program qq;var a, b, max: integer;begin writeln( '); read ( a, b ); max := a; if b > a then

    writeln ( ', max);end.max := b;

  • 2. program qq;var a, b, max: integer;begin writeln( '); read ( a, b ); max := b; if ??? then ??? writeln ( ', max);end.max := a;a > b

  • ?if a > b then begin a := b; else b := a; if a > b then begin a := b; end; else b := a; if a > b then else begin b := a; end;if a > b then a := b; else b := a; end; a := benda := b

    if b >= a then b := a;

  • "4": . : :4 15 9 15"5": . : :4 15 9 56 4 56

  • 3.

  • . 25 40 . , ( ).: , .

  • 1. x'x >= 25?x
  • 1. program qq;var x: integer;begin writeln( '); read ( x ); if x >= 25 then if x
  • 2. x'x >= 25 x
  • 2. program qq;var x: integer;begin writeln( '); read ( x ); if (x >= 25) and (x
  • , (), :not (, )and ( , ', )or ( , ', )xor ( , ) () < >= =

  • notandor, xor=, =, ' . 4 1 6 2 5 3 if not (a > b) or (c d) and (b a) then begin ...end

  • a := 2; b := 3; c := 4;not (a > b)(a < b) and (b < c)not (a >= b) or (c = d)(a < c) or (b < c) and (b < a)(a < b) xor not (b > c) x : (x < 6) and (x < 10)(x < 6) and (x > 10)(x > 6) and (x < 10)(x > 6) and (x > 10)(x < 6) or (x < 10)(x < 6) or (x > 10)(x > 6) or (x < 10)(x > 6) or (x > 10) TrueTrueFALSE(-, 6)(6, 10)(10, )(-, 10)(-, 6) (10,)(-, )(6, )x < 6x > 10x < 10x > 6TrueTrue

  • "4": . : :4"5": ( 1 150 ) , " ". : : :24 57 24 57

  • 4.

  • . ( ). 1 8 ( a b).: 8 .

  • i, i2, i3i
  • ( "")i, i2, i3i2 := i * i;i3 := i2 * i;i := 1,8 ""

  • program qq;var i, i2, i3: integer;begin

    for i:=1 to 8 do begin i2 := i*i; i3 := i2*i; writeln(i:4, i2:4, i3:4); end;end.

  • . 8 1 ( ).: .: ... for i:=8 1 do begin i2 := i*i; i3 := i2*i; writeln(i:4, i2:4, i3:4); end; ...downto

  • for := to do begin { } end; 1:for := downto do begin { } end; 1:

  • : (integer) 1 (to) -1 (downto) , begin end : , (to) ( , )for i:=1 to 8 do writeln(');

  • : (?) :n := 8;for i:=1 to n do begin writeln(''); n := n + 1;end;

  • : , :for i:=1 to 8 do writeln('');writeln('i=', i);for i:=8 downto 1 do writeln('');writeln('i=', i);i=9i=0

  • ?a := 1;for i:=1 to 3 do a := a+1;a = 4a := 1;for i:=3 to 1 do a := a+1;a = 1a := 1;for i:=1 downto 3 do a := a+1;a = 1a := 1;for i:=3 downto 1 do a := a+1;a = 4

  • ?. 1 9.: 2.: 1 -1.: ... for i:=1 to 9 do begin if ??? then begin i2 := i*i; i3 := i2*i; writeln(i:4, i2:4, i3:4); end; end; ...i mod 2 = 1 i

  • ? II: 5 , k 1 5. i 1, i 2.: ... ??? for k:=1 to 5 do begin i2 := i*i; i3 := i2*i; writeln(i:4, i2:4, i3:4); ??? end; ...i := i + 2;i := 1;

  • ? III: 5 , k 1 5. k, i.

    : ... for k:=1 to 5 do begin ??? i2 := i*i; i3 := i2*i; writeln(i:4, i2:4, i3:4); end; ...i := 2*k 1;i = 2k-1

    k12345i13579

  • "4": a b a b. : :4 6 4 16 64 5 25 125 6 36 216"5": 10 : 1, 2, 4, 7, 11, 16, : 1 1 1 2 4 8 4 16 64 ... 46 2116 97336

  • 5.

  • : . ?: (
  • countn 0?count := 0;count := count + 1; n := n div 10; n " n 0"

  • program qq;var n, count: integer;begin writeln( '); read(n); count := 0; while n 0 do begin count := count + 1; n := n div 10; end; writeln(' ', n, ' ', count, ' ');end., n1: integer;n1 := n;n1, " n 0"

  • while do begin { } end;: : , begin end :while (a
  • : , , a := 4; b := 6;while a > b do a := a b;a := 4; b := 6;while a < b do d := a + b;

  • ?a := 4; b := 6;while a < b do a := a + 1;2 a = 6a := 4; b := 6;while a < b do a := a + b;1 a = 10a := 4; b := 6;while a > b do a := a + 1;0 a = 4a := 4; b := 6;while a < b do b := a - b;1 b = -2a := 4; b := 6;while a < b do a := a - 1;

  • for while for i:=1 to 10 do begin { }end;i := 1;while i = b do begin { } i := i - 1;end; while for , . for while .

  • "4": . : :1234 1234 10."5": , , . : : : 1234 1224 . .

  • :1, 2, 3, 4, 5, 1, 2, 4, 7, 11, 16, 1, 2, 4, 8, 16, 32, an = na1 = 1, an+1 = an+1a1 = 1, an+1 = an + nan = 2n-1a1 = 1, an+1 = 2anb1 = 1, bn+1 = bn+1c1 = 2, cn+1 = 2cn

  • : , 0,001: ( 2):b := b+1;c := 2*c;z := -z;

    n12345...b12345...c2481632...z-11-11-1...

  • S|a| > 0.001?S := S + a;S := 0; b := 1; c := 2; z := -1; a := 1; a := z*b/c; b := b + 1; c := 2*c; z := -z;

  • program qq;var b, c, z: integer; S, a: real;begin S := 0; z := -1; b := 1; c := 2; a := 1; while abs(a) > 0.001 do begin S := S + a; a := z * b / c; z := - z; b := b + 1; c := c * 2; end; writeln('S =', S:10:3);end.

  • "4": 0,001:

    :S = 1.157"5": 0,001: :S = 1.220

  • : (
  • : n > 0? " " n

  • program qq;var n: integer;begin repeat writeln( '); read(n); until n > 0; ... { }end.until n > 0; : until (" , ")

  • ?a := 4; b := 6;repeat a := a + 1; until a > b;3 a = 7a := 4; b := 6;repeat a := a + b; until a > b;1 a = 10a := 4; b := 6;repeat a := a + b; until a < b;a := 4; b := 6;repeat b := a - b; until a < b;2 b = 6a := 4; b := 6;repeat a := a + 2; until a < b;

  • ( )"4": , 10. : >= 0: >= 0: -234 1233 . >= 0: 1234 "5": , . : >= 0: >= 0:2323 1234: 2, 3 .

  • 6.

  • : . : :28 2 ()30 4 (), 6 (), 9 (), 11 ()31 1 (), 3 (), 5 (), 7 (), 8 (), 10 (), 12 (): , .

  • MM = 1?D := 31;M = 2?D := 28;M = 12?D := 31; D

  • program qq;var M, D: integer;begin writeln( :'); read ( M ); case M of 2: begin D := 28; end; 4,6,9,11: begin D := 30; end; 1,3,5,7,8,10,12: D := 31; else D := -1; end; if D > 0 then writeln( ', D, ' .') else writeln( ');end.

  • : case (integer) (char)case i+3 of 1: begin a := b; end; 2: begin a := c; end;end;var c: char;...case c of '': writeln(''); '': writeln(''); else writeln(' ');end;

  • : , begin end case i+3 of 1: a := b; 1: a := c;end;case i+3 of 1: a := b; 2: a := c;end;

  • :, , case i of 1: a := b; 2,4,6: a := c; 10..15: a := d; 20,21,25..30: a := e; else writeln('); end;

  • ?case a of 2: begin a := b; 4: a := c;end;case a of 2: a := b 4: a := cend;;case a of 2..5: a := b; 4: a := c;end;case a of 0..2: a := b; 6..3: a := c;end;3..6:case a+c/2 of 2: a := b; 4: a := c;end;case a of 2: a := b; d := 0; 4: a := c;end;beginend;

  • ( )"4": , . : : : -2 2 : 28 . 11 0 . 30 . 1 . "5": , , . : :12 :25 6 .

  • 7.

  • (0,0)(x,y)XYxy

  • , : Pen ( 1, 255, 0, 0 ); : Brush ( 1, 0, 255, 0 ); : TextColor ( 0, 0, 255 ); R(red)0..255G(green)0..255B(blue)0..2550 1 RGBRGB

  • , Pen (1, 0, 255, 0); Line (x1, y1, x2, y2);Pen (1, 0, 0, 255);Point (x, y);Pen (1, 255, 0, 0); MoveTo (x1, y1); LineTo (x2, y2);LineTo (x3, y3);LineTo (x4, y4);LineTo (x5, y5);

  • Pen (1, 0, 0, 255);Brush (1, 255, 255, 0);Rectangle (x1, y1, x2, y2);Pen (1, 255, 0, 0);Brush (1, 0, 255, 0);Ellipse (x1, y1, x2, y2);Brush (1, 100, 200, 255);Fill (x, y);

  • TextColor (0, 0, 255);Brush (1, 255, 255, 0);Font (20, 30, 600);MoveTo (x, y);writeln ('!');!(x, y)10 : 400 600 30

  • (200, 50)(100, 100)(300, 200)program qq;begin Pen(2, 255, 0, 255); Brush(1, 0, 0, 255); Rectangle(100, 100, 300, 200); MoveTo(100, 100); LineTo(200, 50); LineTo(300, 100); Brush(1, 255, 255, 0); Fill(200, 75); Pen(2, 255, 255, 255); Brush(1, 0, 255, 0); Ellipse(150, 100, 250, 200);end.

  • "4": ""

    "5": ""

  • (x1, y1)(x2, y2)N (N=5)hRectangle (x1, y1, x2, y2);Line( x1+h, y1, x1+h, y2);Line( x1+2*h, y1, x1+2*h, y2);Line( x1+3*h, y1, x1+3*h, y2);...h := (x2 x1) / (N + 1);Rectangle (x1, y1, x2, y2);x := x1 + h;for i:=1 to N do begin Line( round(x), y1, round(x), y2); x := x + h;end;var x, h: real;x x

  • ?(x1, y1)(x2, y2)Brush ( 1, c, c, c );Fill ( ???, ??? );: R = G = B c:x(x-1, y1+1)var c, hc: integer;hc := 255 div (N + 1);c := 0;for i:=1 to N+1 do begin Line (round(x), y1, round(x), y2); Brush (1, c, c, c); Fill (round(x)-1, y1+1); x := x + h; c := c + hc;end;

  • (x1, y1)(x2, y2)(x3, y2)ah(x3+a, y1)Line( x1+h, y1, x1+h-a, y2);Line( x1+2*h, y1, x1+2*h-a, y2);Line( x1+3*h, y1, x1+3*h-a, y2);...h := (x3 x2) / (N + 1);a := x2 x1;x := x1 + h;for i:=1 to N do begin Line( round(x), y1, round(x-a), y2); x := x + h;end;xx-a

  • (x1, y1)(x2, y2)hxhyyxyLine( x1, y1+hy, x1+hx, y1+hy) ;Line( x1, y1+2*hy, x1+2*hx, y1+2*hy);Line( x1, y1+3*hy, x1+3*hx, y1+3*hy);...hx := (x2 x1) / (N + 1);hy := (y2 y1) / (N + 1);x := x1 + hx; y := y1 + hy;for i:=1 to N do begin Line( x1, round(y), round(x), round(y)); x := x + hx; y := y + hy;end;

  • "4": , .

    "5": , .

  • 8.

  • : y = 3 sin(x) 0 2.: ymax = 3 x = /2 ymin = -3 x = 3/2 : , , .

  • (x,y)XYxy ()(x,y)xy(0,0)(0,0)abk ( )x = a + kxy = b - ky

  • 2h xw program qq;const a = 50; b = 200; k = 50; xmin = 0; xmax = 6.2832;var x, y, h: real; xe, ye, w: integer;begin w := round((xmax - xmin)*k); Line(a-10, b, a+w, b); Line(a, 0, a, 2*b); x := xmin; h := 0.05; while x
  • ?: (x,y) (x,y): var first: boolean; ...begin ... first := True; while x
  • "4": y = x2 [-3,3].

    "5": ()

  • 9.

  • : :: .: , : ,

  • , .: ( )

  • : ( ) (, , ) (, ) , (x+100, y)(x, y-60)procedure Tr( x, y, r, g, b: integer);begin MoveTo(x, y); LineTo(x, y-60); LineTo(x+100, y); LineTo(x, y); Brush(1, r, g, b); Fill(x+20, y-20);end;

  • program qq;

    begin Pen(1, 255, 0, 255); Tr(100, 100, 0, 0, 255); Tr(200, 100, 0, 255, 0); Tr(200, 160, 255, 0, 0);end.(100,100)10060 procedure Tr( x, y, r, g, b: integer);begin ... end;

  • : , , ( ) procedure Tr( x, y, r, g, b: integer);Tr (200, 100, 0, 255, 0);xyrgb

  • : , , procedure A (x: real; y: integer; z: real);procedure A (x, z: real; y, k, l: integer);

  • : , program qq; procedure A(x, y: integer); var a, b: real; begin a := (x + y)/6; ... end;begin ... end.

  • -: , .:, , program qq;var x, y: integer;

    begin x := 1; y := 2; Exchange ( x, y ); writeln ( x = , x, y = , y ); end;procedure Exchange ( a, b: integer );var c: integer;begin c := a; a := b; b := c;end; x = 1 y = 2

  • -: ( ) , Exchange ( 2, 3 ); { }Exchange ( x+z, y+2 ); { }procedure Exchange ( a, b: integer );var c: integer;begin c := a; a := b; b := c;end;var

  • "4": , .

    "5": , .

  • 10.

  • , . , . , . , : : :: :

  • N N-1 , 2 90o.6 :

  • : ? , !(x1, y1)(x0, y0)+45o-45oLx1 = x0 + Lcos()y1 = y0 Lsin() "" + /4 /4

  • procedure Pifagor(x0, y0, a, L: real; N: integer);const k = 0.6; { }var x1, y1: real; { }begin if N > 0 then begin x1 := x0 + L*cos(a); y1 := y0 - L*sin(a); Line (round(x0), round(y0), round(x1), round(y1)); Pifagor (x1, y1, a+pi/4, L*k, N-1); Pifagor (x1, y1, a-pi/4, L*k, N-1); end;end; , N=0 , .

  • program qq; procedure Pifagor(x0, y0, a, L: real; N: integer); ... end; begin Pifagor (250, 400, pi/2, 150, 8);end; x0y0 Pifagor (250, 400, 2*pi/3, 150, 8);

  • "4": , :

    "5": , :

  • 11.

  • (. animation) .: 400 400 20 20 . , Esc .: ?: (x,y) : (x,y) (x,y) 1

  • "" ? - ( , ).IsEvent , , - .Event , , .if IsEvent then begin Event(k, x, y); if k = 1 then writeln(' ', x) else { k = 2 } writeln(': x=', x, ' y=', y); end;var k, x, y: integer;

  • Esc?program qq;var stop: boolean; k,code,i: integer; begin stop := False; repeat if IsEvent then begin Event(k, code, i); if (k = 1) and (code = 27) then stop := True; end; ... until stop;end; - ... ? 27 (Esc), True,

  • ( )procedure Draw(x, y: integer; flag: boolean);begin if flag then Brush(1, 255, 255, 0) else Brush(1, 0, 0, 255); Rectangle(x, y, x+20, y+20);end;(x, y)(x+20, y+20) = ( ) (True) (False)?: : !

  • program qq;var x, y, k, code, i: integer; stop: boolean; procedure Draw(x,y: integer; flag: Boolean); begin ... end;begin Brush(1, 0, 0, 255); Rectangle(10, 10, 400, 400); Pen(0, 0, 0, 255); x := 10; y := 200; stop := false; repeat if IsEvent then begin ... end; Draw(x, y, True); Delay(10); Draw(x, y, False); x := x + 1; if x >= 400-20 then stop := true; until stop;end. Esc 10

  • "4": :

    "5": :

  • : -. : 37 38Esc 27 39 40: ?:if { } then begin if { } then begin { - code} if code = 37 then x := x 1; if code = 38 then y := y 1; if code = 39 then x := x + 1; if code = 40 then y := y + 1; if code = 27 then stop := True; end;end;IsEventEvent ( k, code, i); if k = 1 then begincase code of 37: x := x 1; 38: y := y 1; 39: x := x + 1; 40: y := y + 1; 27: stop := True;end; ,

  • program qq;var x, y, k, code, i: integer; stop: boolean;

    begin ... repeat Draw(x, y, True); Delay(20); Draw(x, y, False);

    until stop;end.procedure Draw(x,y: integer; flag: Boolean);begin ...end;if IsEvent then begin ...end;

  • ?: , 20 (!) : , : : ? : " , ":while not IsEvent do;

  • program qq;var x, y, k, code, i: integer; stop: boolean;

    begin ... repeat Draw(x, y, True); while not IsEvent do;

    until stop;end.procedure Draw(x,y: integer; flag: Boolean);begin ...end;Draw(x, y, False); Event(k, code, i);...while not IsEvent do;

  • "4": , :

    "5": , :

  • : .: , ?: ( ) (x0, y0)L(x, y)x = x0 + Lcos()y = y0 Lsin()

  • procedure Draw(x, y: integer; flag: boolean);const r = 10;begin if flag then Brush(1, 100, 100, 255) else Brush(1, 0, 0, 0); Ellipse(x-r, y-r, x+r, y+r);end; (True) (False)?: : ! (x-r, y-r)(x,y)(x+r, y+r)

  • program qq;const rSun = 60; { } L = 150; { } x0 = 200; { } y0 = 200; var x, y, { } k, code, i: integer; { Event } a, ha: real; { , } stop: boolean; { }

    begin ...end.procedure Draw(x, y: integer; flag: Boolean);begin ...end;

  • program qq;...begin Brush(1, 0, 0, 0); Fill(1,1); Brush(1, 255, 255, 0); Ellipse(x0-rSun, y0-rSun, x0+rSun, y0+rSun); a := 0; ha := 1*pi/180; { , 1o 100 } stop := false; Pen(0,0,0,0); { } repeat x := round(x0 + L*cos(a)); y := round(y0 - L*sin(a)); Draw(x, y, True); Delay(100); Draw(x, y, False);

    a := a + ha; until stop;end. ha 100 if IsEvent then begin Event(k, code, i); if (k = 1) and (code = 27) then stop := true;end;

  • "4": , :

    "5": --:

  • 12.

  • : (" ") , , .: ? :

  • , , .: [0,m) ( ) [0,1]

    :. , , .2. a, c, m 230-1, k = 5

  • : [a,b]

  • : , , () - abab

  • [0,N]: var x: integer; ... x := random ( 100 ); { [0,99] } [0,1] var x: real; ... x := random; { [0,1] }

  • : 400 300 ?x := random ( 400 );y := random ( 300 ); ? random ?Pen (1, random(256), random(256), random(256));Point ( x, y );

  • program qq;var x, y, k, code, i: integer; stop: boolean;begin stop := False; repeat x := random(400); y := random(300); Pen(1, random(256), random(256), random(256)); Point(x, y ); if IsEvent then begin Event(k, code, i); if (k = 1) and (code = 27) then stop := True; end; until stop;end. Esc

  • "4": .

    "5": ( ). : 45.(100,100)(300,200)

  • 13.

  • (), . : , , ( ?) ?

  • : , , : function Max (a, b: integer): integer;begin if a > b then Max := a else Max := b; end.

  • : function , -

  • : , , , ; :

  • program qq;var a, b, max: integer;

    begin writeln( '); read(a, b); max := Max ( a, b ); writeln( ', max );end.function Max (a, b: integer): integer;begin ... end;ccc

  • : , , , .: (True False) (if, while): 2 N-1, . count := 0;for i := 2 to N-1 do if N mod i = 0 then count := count + 1;if count = 0 then { N }else { N }

  • program qq;var N: integer;

    begin writeln( '); read(N); if Prime(N) then writeln(N, ' ') else writeln(N, ' ');end.function Prime (N: integer): boolean;var count, i: integer;begin i := 2; count := 0; while i*i

  • "4": , 1 N . : : 100 = 5050"5": , , N- ( 1- 1 , 2- 2 , 3- 4 , ) : : 28 28- 134217728 .

  • ( 2)"4": , . : : 14 21 (14,21)=7"5": , ( 0.001)

    : : 45 sin(45) = 0.707x !

  • 14.

  • , .: :

  • A315 ()A[1]A[2]A[3]A[4]A[5] A[2] () : 2 : 10

    510152025

    12345

  • ? :

    :

    var A: array[1.. ] of integer;const N=5;Nvar A : array[ 1 .. 5 ] of integer ;

  • : : :var X, Y: array [1..10] of real; C: array [1..20] of char;var Q: array [0..9] of real; C: array [-5..13] of char;var A: array ['A'..'Z'] of real; B: array [False..True] of integer;... A['C'] := 3.14259*A['B']; B[False] := B[False] + 1;

  • ?var a: array[10..1] of integer;... A[5] := 4.5;[1..10]var a: array ['z'..'a'] of integer;... A['B'] := 15;A['b']['a'..'z']var a: array [0..9] of integer;... A[10] := 'X';

  • : : : :const N = 5; var a: array[1..N] of integer; i: integer;for i:=1 to N do begin write('a[', i, ']='); read ( a[i] );end;a[1] = a[2] = a[3] = a[4] = a[5] = 512345613for i:=1 to N do a[i]:=a[i]*2;writeln(' A:');for i:=1 to N do write(a[i]:4); A: 10 24 68 112 26

  • "4": 5 , . : : 4 15 3 10 14 9.200"5": 5 , . : : 4 15 3 10 14 3

  • 15.

  • : .: :{ , }for i:=2 to N do if a[i] > { } then { a[i] }

  • max := a[1]; { , }iMax := 1;for i:=2 to N do { } if a[i] > max then { } begin max := a[i]; { a[i] } iMax := i; { i } end; : ? iMax a[iMax]. max a[iMax] max.a[iMax]

  • program qq;const N = 5;var a: array [1..N] of integer; i, iMax: integer;begin writeln( :'); for i:=1 to N do begin a[i] := random(100) + 50; write(a[i]:4); end; iMax := 1; { , } for i:=2 to N do { } if a[i] > a[iMax] then { } iMax := i; { i } writeln; { } writeln(' a[', iMax, ']=', a[iMax]);end; [50,150)

  • "4": 10 [-10..10] . : : 4 -5 3 10 -4 -6 8 -10 1 0 a[4]=10 a[8]=-10"5": 10 [-10..10] . : : 4 -5 3 10 -4 -6 8 -10 1 0 a[4]=10, a[7]=8

  • 16.

  • : .: A[1] A[N], A[2] A[N-1], :for i:=1 to N do { A[i] A[N+1-i] } N+1N div 2do

    3597

    7953

    12N-1N

    12N-1N

  • ?231: .: .46?464xycc := x;x := y;y := c;x := y;y := x;321

  • program qq;const N = 10;var A: array[1..N] of integer; i, c: integer;begin { } { } for i:=1 to N div 2 do begin c:=A[i]; A[i]:=A[N+1-i]; A[N+1-i]:=c; end; { }end;

  • "4": 10 [-10..10] 1- 2- . : : 4 -5 3 10 -4 -6 8 -10 1 0 : -4 10 3 -5 4 0 1 -10 8 -6 "5": 12 [-12..12] . : : 4 -5 3 10 -4 -6 8 -10 1 0 5 7 : 10 3 -5 4 -10 8 -6 -4 7 5 0 1

  • : 1 , .:A[1]:=A[2]; A[2]:=A[3]; A[N-1]:=A[N];:for i:=1 to N-1 do A[i]:=A[i+1]; N?

    358197

    1234N-1N

    581973

  • program qq;const N = 10;var A: array[1..N] of integer; i, c: integer;begin { } { } c := A[1]; for i:=1 to N-1 do A[i]:=A[i+1]; A[N] := c; { }end;

  • "4": 10 [-10..10] . : : 4 -5 3 10 -4 -6 8 -10 1 0 : 0 4 -5 3 10 -4 -6 8 -10 1"5": 12 [-12..12] 4 . : : 4 -5 3 10 -4 -6 8 -10 1 0 5 7 : -4 -6 8 -10 1 0 5 7 4 -5 3 10

  • 17.

  • ( , , , , ). : .: , , " (Quick Sort) " (Heap Sort) O(N2) O(NlogN)

  • . ("") (""). , ; , 1 ( ) 1- 2- 3- N N-1 ( N-1 ).

    5213

    5213

    5123

    1523

    1523

    1523

    1253

    1253

    1235

  • 1- : A[N-1] A[N], A[N-2] A[N-1] A[1] A[2] A[j] A[j+1]2- for j:=N-1 downto 2 do if A[j] > A[j+1] then begin c:=A[j]; A[j]:=A[j+1]; A[j+1]:=c; end;2for j:=N-1 downto 1 do if A[j] > A[j+1] then begin c:=A[j]; A[j]:=A[j+1]; A[j+1]:=c; end;1i- for j:=N-1 downto i do ...i

    5263

    12N-1N

    1536

    12N-1N

  • program qq;const N = 10;var A: array[1..N] of integer; i, j, c: integer;begin { } { } for i:=1 to N-1 do begin for j:=N-1 downto i do if A[j] > A[j+1] then begin := A[j]; A[j] := A[j+1]; A[j+1] := ; end; end; { }end;i A[i]

  • , . : -, , ; False, .repeat flag := False; { } for j:=N-1 downto 1 do if A[j] > A[j+1] then begin := A[j]; A[j] := A[j+1]; A[j+1] := ; flag := True; { } end;until not flag; { flag=False }flag := False;flag := True;not flag;var flag: boolean;

    2143

    1234

  • i := 0;repeat i := i + 1; flag := False; { } for j:=N-1 downto 1 do if A[j] > A[j+1] then begin := A[j]; A[j] := A[j+1]; A[j+1] := ; flag := True; { } end;until not flag; { flag=False }i := 0;ii := i + 1;

  • : ( A[1]) ( A[2]), ..

    4312

    1342

    1243

    1243

  • for i := 1 to N-1 do begin nMin = i ; for j:= i+1 to N do if A[j] < A[nMin] then nMin:=j; if nMin i then begin c:=A[i]; A[i]:=A[nMin]; A[nMin]:=c; end;end;N-1N N-1 A[i] A[N] , i+1i

  • "4": 10 [0..100] . : : 14 25 13 30 76 58 32 11 41 97 : 30 11 41 32 13 14 25 76 97 58 "5": 10 [0..100] , . : : 14 25 13 30 76 58 32 11 41 97 : 13 14 25 30 76 97 58 41 32 11

  • 18.

  • , X, , . : : (): ? ? "?

  • nX := 0;for i:=1 to N do if A[i] = X then begin nX := i; break; { } end; nX := 0; { ...} for i:=1 to N do { } if A[i] = X then { , ... } nX := i; { ... } if nX < 1 then writeln(' ...') else writeln('A[', nX, ']=', X);nX : , X, .nX := 0; i := 1;while i
  • X = 7X < 884X > 46X > 6 A[c] X. X = A[c], (). X < A[c], . X > A[c], .

    12345678910111213141516

    12345678910111213141516

    12345678910111213141516

  • nX := 0; L := 1; R := N; {: A[1] A[N] } while R >= L do begin c := (R + L) div 2; if X = A[c] then begin nX := c; R := L - 1; { break; } end; if x < A[c] then R := c - 1; if x > A[c] then L := c + 1; end; if nX < 1 then writeln( ...') else writeln('A[', nX, ']=', X);

    1LcRN

  • N = 222N = 16165N = 1024102411N= 1048576104857621N N log2N+1

  • "4": , , X ( ). .

    "5": , 32 [0,100]. 1000 .

  • 19.

  • ?var B: array[1..N] of char; : ; N, :

  • s[1]s[2]s[3]s[4]var s: string;var s: string[20]; :n := length ( s );var i: integer;

    !

    6

    1

    255

    20

    1

  • : "" "".program qq;var s: string; i: integer;begin writeln( '); readln(s); for i:=1 to Length(s) do if s[i] = '' then s[i] := ''; writeln(s); end.readln(s);writeln(s);Length(s)

  • "4": "" "" , , . : : : "5": , ( ). : : : : : : . .

  • : . :var s, s1, s2: string;s := '';s1 := '';s2 := '';s := s1 + ', ' + s2 + '!';', !': .s := '123456789';

    s1 := Copy ( s, 3, 6 ); s2 := Copy ( s1, 2, 3 ); '345678''456' 3- 6

  • : :s := '123456789';Delete ( s, 3, 6 ); 3- 6 !'123456789''129's := '123456789';Insert ( 'ABC', s, 3 );Insert ( 'Q', s, 5 ); 3- '12ABC3456789''12ABQC3456789'

  • :s := .';n := Pos ( ', s );if n > 0 then writeln( - s[', n, ']')else writeln( ');n := Pos ( '', s );s1 := Copy ( s, n, 4 );var n: integer;s[3]3n = 11: , , 0 ( )

  • s := ' ';n := Pos ( '', s );Delete ( s, n, 4 );Insert ( ', s, n );' 's := ' ';n := length ( s );s1 := Copy ( s, 1, 4 );s2 := Copy ( s, 11, 4 );s3 := Copy ( s, 6, 4 );s := s3 + s1 + s2;n := length ( s );' '14''''''''126

  • : , . -". : , : : .. : " , , ,

  • program qq;var s, name, otch: string; n: integer;begin writeln( , '); readln(s); n := Pos(' ', s); name := Copy(s, 1, n-1); { } Delete(s, 1, n); n := Pos(' ', s); otch := Copy(s, 1, n-1); { } Delete(s, 1, n); { } s := s + ' ' + name[1] + '.' + otch[1] + '.'; writeln(s); end.

  • "4": (, ) ".exe". : : : qqq qqq.com : : qqq.exe qqq.exe"5": "" , : : C:\ \10-\\qq.exe : C: 10- qq.exe

  • 20.

  • : "-" , , . , , . K .1K - 4- 4 4 4 4 :

  • 1K: 4- K-1 .1K1K1K

  • procedure Rec(p: integer);begin if p > K then begin writeln(s); count := count+1; end else begin s[p]:=''; Rec ( p+1 ); s[p]:=''; Rec ( p+1 ); s[p]:=''; Rec ( p+1 ); s[p]:=''; Rec ( p+1 ); end;end; 1Kp : var s: string; count, K: integer;sp+1

    ????

  • procedure Rec(p: integer); const letters = ''; var i: integer; begin if p > k then begin writeln(s); count := count+1; end else begin for i:=1 to length(letters) do begin s[p] := letters[i]; Rec(p+1); end; end; end; const letters = ';for i:=1 to length(letters) do begin s[p] := letters[i]; Rec(p+1);end;

  • program qq;var s: string; K, i, count: integer;begin writeln( :'); read ( K ); s := ''; for i:=1 to K do s := s + ' '; count := 0; Rec ( 1 ); writeln( ', count, ' ');end.procedure Rec(p: integer); ...end;s := '';for i:=1 to K do s := s + ' '; K

  • "-" , , . K ."4": , 1 , . "5": , , (, ), .

  • 21.

  • : .123456c6A[6,3]

    abcdefgh

    87654321

    0000200000000000003000000000000000000040000000000000000000000000

    87654321

    12345678

  • . , ( ).A 2 3 A[3,4]

    147362-50151089111220

    123

    12345

    7011

    2-501510

    12

  • :const N = 3; M = 4;var A: array[1..N,1..M] of integer; B: array[-3..0,-8..M] of integer; Q: array['a'..'d',False..True] of real; :for i:=1 to N do for j:=1 to M do begin write('A[',i,',',j,']='); read ( A[i,j] ); end;A[1,1]=25A[1,2]=14A[1,3]=14...A[3,4]=54ijfor j:=1 to M do for i:=1 to N do begin

  • for i:=1 to N do for j:=1 to M do A[i,j] := random(25) - 10; for i:=1 to N do begin for j:=1 to M do write ( A[i,j]:5 ); writeln;end;

    1225113156112447145622223

  • : 3 4 . .program qq;const N = 3; M = 4;var A: array[1..N,1..M] of integer; i, j, S: integer;begin ... { } S := 0; for i:=1 to N do for j:=1 to M do S := S + A[i,j]; writeln( ', S);end;

  • 8 5 [-10,10] ."4": . : A[3,4]=-6 A[2,2]=10"5": , . : 2: 3 5 8 9 8

  • 1. N N .A[1,N]A[2,2]A[3,3]A[N,N]for i:=1 to N do write ( A[i,i]:5 ); 2. .A[N,1]A[N-1,2]A[2,N-1]for i:=1 to N do write ( A[i, ]:5 );N+1-i N+1A[1,1]

  • 3. , . 1: A[1,1] 2: A[2,1]+A[2,2]... N: A[N,1]+A[N,2]+...+A[N,N]S := 0;for i:= 1 to N do for j:= 1 to i do S := S + A[i,j]; i

  • 4. . N M 2- 4- .24jA[2,j]A[4,j]for j:=1 to M do begin c := A[2,j]; A[2,j] := A[4,j]; A[4,j] := c;end; 5. .for i:=1 to N do A[i,3] := A[i,3] + A[i,6];

    12521

    73137

  • 7 7 [-10,10] . , , ."4": "5":

  • 22.

  • , . , ( < 32)ACSII (1 )UNICODE (2 )*.txt, *.log,*.htm, *.html - *.doc, *.exe,*.bmp, *.jpg,*.wav, *.mp3,*.avi, *.mpg ()

  • I . : f ( , )assign(f, 'qq.dat');reset(f); { }rewrite(f); { }II : ": var f: text;III : close(f); read ( f, n ); { n }write ( f, n ); { n }writeln ( f, n );{ }

  • : assign, , , , , , f

  • , 12 5 45 67 56 (end of file, EOF) 12 5 45 67 56assign ( f, 'qq.dat' );reset ( f );read ( f, x );

  • ? close ( f ); reset ( f ); { } readln ( f, x ); 12 5 45 36 67 56 (end of line, EOL)

  • : input.txt ( ), . output.txt .: input.txt .S := 0; , 7. x.S := S + x; 3. input.txt. output.txt . S. output.txt. "

  • program qq;var s, x: integer; f: text; begin assign(f, 'input.txt'); reset(f); s := 0; while not eof(f) do begin readln(f, x); s := s + x; end; close(f); assign(f, 'output.txt'); rewrite(f); writeln(f, ' ', s); close(f);end.f: text;eof(f) , True, output.txt

  • input.txt , . "4": output.txt."5": output.txt.

  • : input.txt ( ), , 100. output.txt.: (); .: 100 ; N; N ; .

  • var A: array[1..100] of integer; f: text; function ReadArray: integer; var i: integer; begin assign(f, 'input.txt'); reset(f); i := 0; while (not eof(f)) and (i < 100) do begin i := i + 1; readln(f, A[i]); end; close(f); ReadArray := i; end; :: , ReadArray := i; , 100

  • program qq;var A: array[1..100] of integer; f: text; N: integer;Begin N := ReadArray; ... { N } assign(f, 'output.dat'); rewrite(f); for i:=1 to N do writeln(f, A[i]); close(f);end.function ReadArray: integer;...end;

  • input.txt ( ), , 100. "4": output.txt."5": output.txt.

  • : input.txt , - "". output.txt. input.txt : , , , , . , , , , . , , , , . , , , , , ... - output.txt : . . . , .

  • : (readln). ", ," (Pos, Delete). 1. s:: ( , ).

    repeat i := Pos(', ,', s); if i 0 then Delete(s, i, 9); until i = 0; ", ," 9

  • program qq; var s: string; i: integer; fIn, fOut: text; begin assign(fIn, 'instr.txt'); reset(fIn); assign(fOut, 'outstr.txt'); rewrite(fOut); ... { } close(fIn); close(fOut); end.fIn, fOut: text;

  • while not eof(fIn) do begin readln(fIn, s); writeln(fOut, s); end; repeat i := Pos(', ,', s); if i 0 then Delete(s, i, 9); until i = 0; ""

  • input.txt , . "4": "" " " output.txt."5": output.txt , 5 ( ).

  • ******************************************************************************************************************************************************************************************************************************************************