of 41/41
SBD Hä vµ tªn V¨n To¸n Anh Tæng KÕt qu¶ 105 Lª ThÞ Thu 8.5 10.0 7.0 9.0 102 Vò Ngäc S¬n 6.0 8.5 8.5 5.0 215 TrÇn Thuû 7.0 7.0 6.5 6.5 211 NguyÔn Anh 4.5 5.0 7.0 7.5 245 Phan V©n 5.0 2.0 3.5 4.5 VÝ dô 1: Qu¶n lÝ ®iÓm trong mét k× thi b»ng m¸y tÝnh. Yªu cÇu : H·y x¸c ®Þnh th«ng tin ®-a vµo (Input) vµ th«ng tin cÇn lÊy ra (Output) Input: SBD, Hä vµ tªn, V¨n, To¸n, LÝ, Anh. Output: Tæng ®iÓm, KÕt qu¶ thi cña häc sinh. 53 §ç 42.5 §ç 41 §ç 33.5 §ç 22

Bai 4 bai toan va thuat toan main

  • View
    127

  • Download
    4

Embed Size (px)

Text of Bai 4 bai toan va thuat toan main

  1. 1. SBD H v tn Vn Ton L Anh Tng Kt qu 105 L Th Thu 8.5 10.0 7.0 9.0 102 V Ngc Sn 6.0 8.5 8.5 5.0 215 Trn Thu 7.0 7.0 6.5 6.5 211 Nguyn Anh 4.5 5.0 7.0 7.5 245 Phan Vn 5.0 2.0 3.5 4.5 V d 1: Qun l im trong mt k thi bng my tnh. Yu cu : Hy xc nh thng tin a vo (Input) v thng tin cn ly ra (Output) Input: SBD, H v tn, Vn, Ton, L, Anh. Output: Tng im, Kt qu thi ca hc sinh. 53 42.5 41 33.5 22
  2. 2. V d 2: Gii phng trnh bc nht ax + b = 0. Yu cu : Hy xc nh thng tin a vo (Input) v thng tin cn ly ra (Output) Input: Cc h s a, b. Output: Nghim ca phng trnh. Vi a = 1, b = -5 Phng trnh c nghimx = 5
  3. 3. 1. Khi nimbi ton L vic notamunmythc hintthngtin avo(INPUT) tmc thngtinra(OUTPUT). V d 3: Tm c s chung ln nht ca hai s nguyn dng. INPUT: Hai s nguyn dng Mv N. OUTPUT: c s chung ln nht ca Mv N. V d 4: Bi ton xp loi hc tp ca mt lp. INPUT: Bng imca hc sinh trong lp. OUTPUT: Bng xp loi hc lc ca hc sinh. Bi 4. Bi ton v thut TonBi 4. Bi ton v thut Ton
  4. 4. 2. Khi nimthut ton TINPUTlm thno tm raOUTPUT? Cc emcn tm ra cch gii ca bi ton.
  5. 5. Xt v d 2: Gii phng trnh bc nh t ax + b = 0. B1: Xc nh hs a, b;B1: Xc nh hs a, b; B2: Nu a=0 v b=0 => Phng trnh vs nghim =>B5;B2: Nu a=0 v b=0 => Phng trnh vs nghim =>B5; B3: Nu aB3: Nu a==0 v b0 v b0 => Phng trnh vnghim=>B5;0 => Phng trnh vnghim=>B5; B4: Nu aB4: Nu a0 => Phng trnh c nghim x=-b/a =>B5;0 => Phng trnh c nghim x=-b/a =>B5; B5: Kt thc.B5: Kt thc.
  6. 6. Thut ton gii mt bi ton l mt dy hu hn cc thao tc c sp xp theo mt trnh t xc nh sao cho saukhi thc hindythao tc y, t Input ca biton, tanhnc Output cntm. C hai cch thhin mt thut ton: Cch 1: Lit k cc bc. Cch 2: V s khi.
  7. 7. B7: Kt thc. B1: Bt u;B1: Bt u; B2: Nhp a, b, c;B2: Nhp a, b, c; B3: TnhB3: Tnh = b= b22 4ac; 4ac; B4: NuB4: Nu < 0 => PT vnghim=> B7;< 0 => PT vnghim=> B7; B5: NuB5: Nu = 0= 0 => PT c nghimkp x = -b/2a => B7;=> PT c nghimkp x = -b/2a => B7; B6: NuB6: Nu > 0> 0 => PT c hai nghimx1, x2 = (-b=> PT c hai nghimx1, x2 = (-b )/2a)/2a => B7;=> B7; 3. Mt s v d v thut ton Thut ton gii phng trnh bc hai (a 0). Cch 1: Lit k cc bc
  8. 8. Quy c cc khi trong s thut tonQuy c cc khi trong s thut ton Bt u thut ton. Dng nhp v xut d liu. Dng gn gi trv tnh ton. Xt iu kin r nhnh theo mt trong hai iu kin ng, sai. Kt thc thut ton. B K S KT Cch 2: V s khi
  9. 9. Nhp vo a, b, c = b - 4ac < 0 PT vnghim = 0 PT c nghimx= - b/2a KT BD s S thut ton gii phng trnh bc haiS thut ton gii phng trnh bc hai 2 PT c 2 nghim x1,x2= ( -b )/2a B1 B2 B3 B4 B5 B6 s B7
  10. 10. a,b,c= 1 3 5 = 33 45 = 11 11 < 0 PT vnghim = 0 PT c nghimx = -b/2a KT BD -11 531 cba S PT c 2 nghim x1, x2 = (-b )/2a S = b* b 4* a* c nhp vo a,b,c < 0 Mphng thut ton gii phng trnh bc haiMphng thut ton gii phng trnh bc hai B TEST 1:
  11. 11. a,b,c= 1 2 1 = 22 411 = 0 PT vnghim PT c nghimx=-b/2a KT BD 0 121 cba S PT c 2 nghim x1, x2 = (-b )/2a S = b* b 4* a* c nhp vo a,b,c < 0 Mphng thut ton gii phng trnh bc haiMphng thut ton gii phng trnh bc hai B TEST 2: = 0 PT c nghim kp x=-1
  12. 12. a,b,c= 1 -5 6 = 25 24 = 1 PT vnghim PT c nghimx=-b/2a KT BD 1 6-51 cba S PT c 2 nghim x1, x2 = (-b )/2a S = b* b 4* a* c nhp vo a,b,c < 0 Mphng thut ton gii phng trnh bc haiMphng thut ton gii phng trnh bc hai B TEST 3: = 0 PT c nghimx1 = 3 x2 = 2
  13. 13. Thut ton tm max 3 Ngi ta t 5 qu bng c kch thc khc nhau trong hp c ynp nh hnhbn. Chdngtayhytmra qu bngckchthc lnnht .
  14. 14. Qu ny ln nht Qu ny mi ln nht ! Qu ny ln hn Tm ra qu ln nht ri! Cng tm thut ton MAX
  15. 15. Thut ton tms ln nht trong mt dy s nguyn Xc nh bi ton: INPUT: S nguyn dng N v dy N s nguyn a1, a2, , aN (ai vi i: 1N). OUTPUT: S ln nht (Max) ca dy s.
  16. 16. tng: - t gi trMax = a1. - Ln lt cho i chy t 2 n N, so snh gi trai vi gi trMax, nu ai > Max th Max nhn gi trmi l ai.
  17. 17. Cch 1: Lit k cc bcCch 1: Lit k cc bc B1: Nhp N v dy aB1: Nhp N v dy a11,, a,, aNN;; B2: MaxB2: Max aa11; i; i 2;2; B3: Nu i > N th a ra gi trMax ri kt thc;B3: Nu i > N th a ra gi trMax ri kt thc; B4:B4: Bc 4.1: Nu aBc 4.1: Nu aii > Max th Max> Max th Max aaii;; Bc 4.2: iBc 4.2: i i+1 ri quay li B3.i+1 ri quay li B3.
  18. 18. S S Nhp N v dy a1,,aN Max a1 ; i 2 i > N ? ai > Max ? Max ai i i + 1 a ra Max ri kt thc B1: Nhp N v dy a1,,aN; B2: Max a1; i 2; B3: Nu i > N th a ra gi tr Max ri kt thc; B4 : 4.1: Nu ai > Max th Max ai; 4.2: i i + 1 ri quay li B3. Cch 2: S khiCch 2: S khi
  19. 19. S S Nhp N v dy a1,,aN Max a1 ; i 2 I > N ? ai> Max ? Max ai i i+1 a ra Max ri kt thc Max i A 77555 5432 67415N=5 ; A [ 5 1 4 7 6 ] Max 5 ; i 2 2 > 5 ? 1> 5 ? i 2+1 3 > 5 ? 4> 5 ? i 3+1 4 > 5 ? 7 > 5 ? Max 7 4 i 4+1 5 > 5 ? 7 > 7 ? i 5+1 6 > 5 ? S ln nht ca dy l 7 Mphng thut ton Vi i = 2Vi i = 3Vi i = 4Vi i = 5
  20. 20. S S Nhp N v dy a1,,aN Max a1 ; i 2 I > N ? ai> Max ? Max ai i i+1 a ra Max ri kt thc Max i A 77555 5432 67415N=5 ; A [ 5 1 4 7 6 ] Max 5 ; i 2 2 > 5 ? 1> 5 ? i 2+1 3 > 5 ? 4> 5 ? i 3+1 4 > 5 ? 7 > 5 ? Max 7 4 i 4+1 5 > 5 ? 7 > 7 ? i 5+1 6 > 5 ? S ln nht ca dy l 7
  21. 21. Thut ton kimtra tnh nguyn t ca mt s nguyn dng Xc nh bi ton: INPUT: N l mt s nguyn dng. OUTPUT: Tr li cu hi N c l s nguyn t khng?
  22. 22. tng: Xt cc trng hp Cc emhy nu nh ngha s nguyn t. - Nu N 4 v khng c c s trong phmvi t 2 n phn nguyn cn bc hai ca N th N l s nguyn t. - Nu N = 1 th N khng l s nguyn t. - Nu 1< N [B5: Nu i > [N ] th thng bo N l nguyn t, kt thc;N ] th thng bo N l nguyn t, kt thc; B6: Nu N chia ht cho i th thng bo N khng nguynB6: Nu N chia ht cho i th thng bo N khng nguyn tt ri kt thc;ri kt thc; B7: iB7: i i +1 ri quay li B5.i +1 ri quay li B5.
  23. 25. Nhp N N =1 ? N < 4 ? i 2 i>[N ] ? N c chia ht cho i ? i i +1 Thng bo N l s nguyn t ri kt thc. Thng bo N khng l s nguyn t ri kt thc. S S S S Cch 2 V s khi
  24. 26. Thut ton sp xp Hytmcchspxphc sinhngcho c(hnha) theo th tthptrc cao sau(hnhb). Hnha Hnhb
  25. 27. Thut ton sp xp bng tro i Xc nh bi ton: INPUT: Dy A gm N s nguyn a1, a2,, aN. OUTPUT: Dy A c sp xp thnh dy khng gim.
  26. 28. tng: Vi mi cp s hng ng lin k trong dy, nu s trc ln hn s sau ta i v tr chng cho nhau. Vic c lp li cho n khi khng c s i ch no xy ra na.
  27. 29. Vi N = 6 v dy A gm6 s hng nh sau : 3 5 9 8 1 7 Lt th nht: 3 5 9 8 1 7 3 5 8 9 1 7 3 5 8 1 9 7 3 5 8 1 7 9 Lt th hai: 3 5 8 1 7 9 3 5 1 8 7 9 3 5 1 7 8 9 Lt th ba: 3 5 1 7 8 9 3 1 5 7 8 9 3 1 5 7 8 9 1 3 5 7 8 9 Lt th t: Mphng thut ton sp xp bng tro iMphng thut ton sp xp bng tro i
  28. 30. Cch 1: Lit k cc bcCch 1: Lit k cc bc B1: Nhp N, cc s hng aB1: Nhp N, cc s hng a11, a, a22,, a,, aNN;; B2: MB2: M N;N; B3: Nu M< 2 th a ra dy A sp xp ri kt thc;B3: Nu M< 2 th a ra dy A sp xp ri kt thc; B4: MB4: M M 1; iM 1; i 0;0; B5: iB5: i i +1;i +1; B6: Nu i > Mth quay li B3;B6: Nu i > Mth quay li B3; B7: Nu ai > ai+1 th tro i ai v ai+1 cho nhau; B8: Quay li B5.B8: Quay li B5.
  29. 31. Nhp N v a1, a2,..., aN M N M< 2 ? M M - 1; i 0 i i + 1 i > M? ai > ai+1 ? Tro i ai v ai+1 a ra A sp xp ri kt thc S S S Cch 2 V s khi
  30. 32. Thut ton tm kim Hai bn ch (Bi v Bng) chi trn tm, Bng trn vo mt trongnhngchic m canggi Nentrn. Hychracc cch tmchic m m Bngangtrn?Chobit cnhngcchno? Bngtrn unh? C1: Tmkimtunt ( mtngm) C2: Do cc m sp xp ln dn, hai m unhhn ngi caBngnnchtmhai m sauthi!
  31. 33. Thut ton tmkimtun t Xc nh bi ton: INPUT: Dy A gmN s nguyn a1, a2,, aN i mt khc nhau v s nguyn k. OUTPUT: Chs i m ai = khoc thng bo khng c s hng no ca A bng k.
  32. 34. 54321I 5125118924175A Mphng thut ton tmkimtun tMphng thut ton tmkimtun t Vi k= 2 v dy A gm10 s hng nh sau: Ti vtr i = 5 c a5 = 2 = k Vi k= 6 v dy A gm10 s hng nh sau: A 5 7 1 4 2 9 8 11 25 51 I 1 2 3 4 5 6 7 8 9 10 11 Vi mi i t 1 10 khng c ai c gi trbng 6 5
  33. 35. tng: Ln lt t s hng th nht, ta so snh gi tr s hng ang xt vi kho (k) cho n khi c s trng nhau, nu xt ti s hng cui cng m khng c s trng nhau th c ngha l dy A khng c s hng no c gi trbng k.
  34. 36. Cch 1: Lit k cc bcCch 1: Lit k cc bc Bc 1: Nhp N, cc s hng aBc 1: Nhp N, cc s hng a11, a, a22,, a,, aNN v gi trkho k;v gi trkho k; Bc 2: iBc 2: i 1;1; Bc 3: Nu aBc 3: Nu aii = kth thng bo ch s i, ri kt thc;= kth thng bo ch s i, ri kt thc; Bc 4: iBc 4: i i+1;i+1; Bc 5: Nu i > N th thng bo dy A khng c sBc 5: Nu i > N th thng bo dy A khng c s hng no c gi trbng k, ri kt thc;hng no c gi trbng k, ri kt thc; Bc 6: Quay li B3.Bc 6: Quay li B3.
  35. 37. Nhp N, a1, a2,..., aN v k i 1 ai = k? a ra i ri kt thc S i i + 1 i > N ? Thng bo dy A khng c s hng c gi trbng k, ri kt thc S Cch 2 V s khi
  36. 38. Thut ton tmkimnhphn tng: S dng tnh cht dy A sp xp tng, ta tmcch thu hp nhanh phmvi tmkim bng cch so snh kvi s hng gia dy (agia), khi chxy ra mt trong ba trng hp: - Nu agia= k=> tmc chs, kt thc; - Nu agia > k=> do dy A c sp xp tng nn vic tm kimthu hp chxt t a1 agia - 1; - Nu agia < k=> do dy A c sp xp tng nn vic tm kimthu hp chxt t agia + 1 aN. Qu trnh trn c lp i lp li cho n khi tmc OUTPUT.
  37. 39. Mphng thut ton tmkimnhphnMphng thut ton tmkimnhphn 10987654321i 333130222196542A Vi k= 21 v dy A gm 10 s hng nh sau: Lt th nht: a: agiagia l al a55 = 9; 9 = 30; 30 > 21 vng tm kimthu hp trong phm vi t avng tm kimthu hp trong phm vi t a66 aa77;; Lt th baLt th ba: a: agiagia l al a66 = 21;= 21; 21= 2121= 21 Vy s cn tm l i = 6. 2221 66 21
  38. 40. Lit k cc bcLit k cc bc Bc 1: Nhp N, cc s hng aBc 1: Nhp N, cc s hng a11, a, a22,, a,, aNN v gi trkho k;v gi trkho k; Bc 2: uBc 2: u 1, Cui1, Cui N;N; Bc 3: GiaBc 3: Gia [(u + Cui)/2];[(u + Cui)/2]; Bc 4: Nu aBc 4: Nu aGiaGia = kth thng bo ch s Gia= kth thng bo ch s Gia ri kt thc;ri kt thc; Bc 5: Nu aBc 5: Nu aGiaGia > kth t Cui = Gia - 1 ri> kth t Cui = Gia - 1 ri chuyn sang bc 7;chuyn sang bc 7; Bc 6: uBc 6: u Gia + 1;Gia + 1; Bc 7: Nu uBc 7: Nu u Cui th thng bo dy A khng cCui th thng bo dy A khng c s hng c gi trbng k, ri kt thc;s hng c gi trbng k, ri kt thc; Bc 8: Quay li bc 3.Bc 8: Quay li bc 3.
  39. 41. 1. Khi nimbi ton Bi ton v thut TonBi ton v thut Ton 2. Khi nimthut ton Thut ton gii phng trnh bc hai (a 0). Thut ton tmMax ca mt dy s. Thut ton kimtra tnh nguyn t ca mt s nguyn dng. Thut ton sp xp bng tro i. Thut ton tmkimtun t v nhphn.