Funny Programming in Delphi - Hares and PheasantsIntroduction
An hunting event has been organized by local gamekeepers community. At the end of the event all hunters were interested in results, i.e. how many hares and pheasants were shot. Because gamekeepers were extremely tired, they weren't able to distinguish between hare and pheasant. A local teacher got an idea: "We count all shot animals and total figure of their legs. Based on these facts, we should be able to determine particular totals of hares and pheasants.".
- KS - real count of heads
- NOHY - total count of legs
- KUSY - calculated count of heads (changes by every loop pass, at the end it will have the same value as variable KS)
- ZAJ - count of hares (changes by every loop pass, at the end it will have value of real count of hares)
- BAZ - count of pheasants (changes by every loop pass, at the end it will have value of real count of pheasants)
procedure TForm2.Button1Click(Sender: TObject); var Nohy, Ks, Kusy, Baz, Zaj, Test:Integer; begin begin Nohy:=seNohy.Value; Ks:=seKs.Value; if NOHY <= 4*KS //No animal can have more than 4 legs then if NOHY >= 2*KS //No animal can have less than 2 legs then begin TEST := NOHY div (2); //test of divisibility by 2 TEST := TEST*2; if NOHY = TEST then //All the nonsense values are out of question, begin //we can calculate ZAJ := 0; //Initial settings - no hare, BAZ := NOHY div (2); //all pheasants KUSY := ZAJ + BAZ; //We'll try how many heads it would be //While loop while KUSY > KS do //Until there are more test heads than real ones begin ZAJ := ZAJ + 1; //One hare is added BAZ := BAZ - 2; //Two pheasants are removed KUSY := ZAJ + BAZ; //Heads recalculated end; ShowMessage('Hares '+IntToStr(ZAJ)+ ', Pheasants ' +IntToStr(BAZ)); end else //Odd count of legs ShowMessage('Animals with odd count of legs are not accepted:-)'); end else ShowMessage ('Each animal must have at least two legs! ') else ShowMessage ('No animal can have more than four legs! '); end; end;
Our new series of articles focused on practical using of algorithms should show how to use some mathematical and statistical methods for solving real life tasks and issues.
Sample project regarding this topic can be downloaded from here.
About the AuthorIng. Jana Pšenčíková
More interesting links and pages
Discover UK licensed casino sites at Casinopilot
Try out new casino sites at SmartphoneCasinos.co.uk