c++实习的作业
2008-03-16 16:50
323 查看
大一上学期学了c++,下学期要实习,而实习题目就是acm(toj)上的9道题,附加一个纸牌游戏的类
网址:http://acm.tju.edu.cn
Program1:WebNavigation(TOJ_1196)
网址:
Program1:WebNavigation(TOJ_1196)
Standardwebbrowserscontainfeaturestomovebackwardandforwardamongthepagesrecentlyvisited.Onewaytoimplementthesefeaturesistousetwostackstokeeptrackofthepagesthatcanbereachedbymovingbackwardandforward.Inthisproblem,youareaskedtoimplementthis.
Thefollowingcommandsneedtobesupported:
BACK:Pushthecurrentpageonthetopoftheforwardstack.Popthepagefromthetopofthebackwardstack,makingitthenewcurrentpage.Ifthebackwardstackisempty,thecommandisignored.
FORWARD:Pushthecurrentpageonthetopofthebackwardstack.Popthepagefromthetopoftheforwardstack,makingitthenewcurrentpage.Iftheforwardstackisempty,thecommandisignored.
VISIT<url>:Pushthecurrentpageonthetopofthebackwardstack,andmaketheURLspecifiedthenewcurrentpage.Theforwardstackisemptied.
QUIT:Quitthebrowser.
AssumethatthebrowserinitiallyloadsthewebpageattheURL'target='_blank'>http://www.acm.org/[/code] InputInputisasequenceofcommands.ThecommandkeywordsBACK,FORWARD,VISIT,andQUITareallinuppercase.URLshavenowhitespaceandhaveatmost70characters.Youmayassumethatnoprobleminstancerequiresmorethan100elementsineachstackatanytime.TheendofinputisindicatedbytheQUITcommand.OutputForeachcommandotherthanQUIT,printtheURLofthecurrentpageafterthecommandisexecutedifthecommandisnotignored.Otherwise,print"Ignored".Theoutputforeachcommandshouldbeprintedonitsownline.NooutputisproducedfortheQUITcommand.SampleInputVISIT'target='_blank'>http://acm.ashland.edu/[/code] VISIT'target='_blank'>http://acm.baylor.edu/acmicpc/[/code] BACKBACKBACKFORWARDVISIT'target='_blank'>http://www.ibm.com/[/code] BACKBACKFORWARDFORWARDFORWARDQUITSampleOutputhttp://acm.ashland.edu/http://acm.baylor.edu/acmicpc/http://acm.ashland.edu/http://www.acm.org/Ignoredhttp://acm.ashland.edu/http://www.ibm.com/http://acm.ashland.edu/http://www.acm.org/http://acm.ashland.edu/http://www.ibm.com/Ignored
Program2:OctalFractions(TOJ_1079)
Fractionsinoctal(base8)notationcanbeexpressedexactlyindecimalnotation.Forexample,0.75inoctalis0.963125(7/8+5/64)indecimal.Alloctalnumbersofndigitstotherightoftheoctalpointcanbeexpressedinnomorethan3ndecimaldigitstotherightofthedecimalpoint.
Writeaprogramtoconvertoctalnumeralsbetween0and1,inclusive,intoequivalentdecimalnumerals.Theinputtoyourprogramwillconsistofoctalnumbers,oneperline,tobeconverted.Eachinputnumberhastheform0.d1d2d3...dk,wherethediareoctaldigits(0..7).Thereisnolimitonk.Youroutputwillconsistofasequenceoflinesoftheform
0.d1d2d3...dk[8]=0.D1D2D3...Dm[10]
wheretheleftsideistheinput(inoctal),andtherighthandsidethedecimal(base10)equivalent.Theremustbenotrailingzeros,i.e.Dmisnotequalto0.
SAMPLEINPUT0.750.00010.01234567
SAMPLEOUTPUT0.75[8]=0.953125[10]0.0001[8]=0.000244140625[10]0.01234567[8]=0.020408093929290771484375[10]
Program3:Soundex(TOJ_1326)
[align=left][/align]
[align=left]Soundexcodinggroupstogetherwordsthatappeartosoundalikebasedontheirspelling.Forexample,"can"and"khawn","con"and"gone"wouldbeequivalentunderSoundexcoding.[/align]
[align=left]Soundexcodinginvolvestranslatingeachwordintoaseriesofdigitsinwhicheachdigitrepresentsaletter:[/align]
[align=left]1representsB,F,P,orV[/align]
[align=left]2representsC,G,J,K,Q,S,X,orZ[/align]
[align=left]3representsDorT[/align]
[align=left]4representsL[/align]
[align=left]5representsMorN[/align]
[align=left]6representsR[/align]
[align=left]ThelettersA,E,I,O,U,H,W,andYarenotrepresentedinSoundexcoding,andrepeatedletterswiththesamecodedigitarerepresentedbyasingleinstanceofthatdigit.WordswiththesameSoundexcodingareconsideredequivalent.[/align]
[align=left]Eachlineofinputcontainsasingleword,alluppercase,lessthan20letterslong.Foreachlineofinput,producealineofoutputgivingtheSoundexcode.[/align]
[align=left]SampleInput[/align]
[align=left]KHAWN[/align]
[align=left]PFISTER[/align]
[align=left]BOBBY[/align]
[align=left]OutputforSampleInput[/align]
[align=left]25[/align]
[align=left]1236[/align]
[align=left]11[/align]
[align=left][/align]group:A
Program4:Psuedo-RandomNumbers(TOJ_2120)
[align=left]Computersnormallycannotgeneratereallyrandomnumbers,butfrequentlyareusedtogeneratesequencesofpseudo-randomnumbers.Thesearegeneratedbysomealgorithm,butappearforallpracticalpurposestobereallyrandom.Randomnumbersareusedinmanyapplications,includingsimulation.[/align]
[align=left]Acommonpseudo-randomnumbergenerationtechniqueiscalledthelinearcongruentialmethod.Ifthelastpseudo-randomnumbergeneratedwasL,thenthenextnumberisgeneratedbyevaluating(Z×L+I)modM,whereZisaconstantmultiplier,Iisaconstantincrement,andMisaconstantmodulus.Forexample,supposeZis7,Iis5,andMis12.Ifthefirstrandomnumber(usuallycalledtheseed)is4,thenwecandeterminethenextfewpseudo-randomnumbersarefollows:[/align]
[align=left]LastRandomNumber,L|(Z×L+I)|NextRandomNumber,(Z×L+I)modM[/align]
[align=left]----------------------|---------|----------------------------------[/align]
[align=left]4|33|9[/align]
[align=left]9|68|8[/align]
[align=left]8|61|1[/align]
[align=left]1|12|0[/align]
[align=left]0|5|5[/align]
[align=left]5|40|4[/align]
[align=left]Asyoucansee,thesequenceofpseudo-randomnumbersgeneratedbythistechniquerepeatsaftersixnumbers.Itshouldbeclearthatthelongestsequencethatcanbegeneratedusingthistechniqueislimitedbythemodulus,M.[/align]
[align=left]InthisproblemyouwillbegivensetsofvaluesforZ,I,M,andtheseed,L.Eachofthesewillhavenomorethanfourdigits.Foreachsuchsetofvaluesyouaretodeterminethelengthofthecycleofpseudo-randomnumbersthatwillbegenerated.Butbecareful--thecyclemightnotbeginwiththeseed![/align]
[align=left]Input[/align]
[align=left]Eachinputlinewillcontainfourintegervalues,inorder,forZ,I,M,andL.Thelastlinewillcontainfourzeroes,andmarkstheendoftheinputdata.LwillbelessthanM.[/align]
[align=left]Output[/align]
[align=left]Foreachinputline,displaythecasenumber(theyaresequentiallynumbered,startingwith1)andthelengthofthesequenceofpseudo-randomnumbersbeforethesequenceisrepeated.[/align]
[align=left]SampleInput[/align]
[align=left]75124[/align]
[align=left]5173384932791511[/align]
[align=left]9111530960001234[/align]
[align=left]1079213699991237[/align]
[align=left]0000[/align]
[align=left]SampleOutput[/align]
[align=left]Case1:6[/align]
[align=left]Case2:546[/align]
[align=left]Case3:500[/align]
[align=left]Case4:220[/align]group:B
Program1:Transmitters(TOJ_1916)
Inawirelessnetworkwithmultipletransmitterssendingonthesamefrequencies,itisoftenarequirementthatsignalsdon'toverlap,oratleastthattheydon'tconflict.Onewayofaccomplishingthisistorestrictatransmitter'scoveragearea.Thisproblemusesashieldedtransmitterthatonlybroadcastsinasemicircle.
AtransmitterTislocatedsomewhereona1,000squaremetergrid.Itbroadcastsinasemicircularareaofradiusr.Thetransmittermayberotatedanyamount,butnotmoved.GivenNpointsanywhereonthegrid,computethemaximumnumberofpointsthatcanbesimultaneouslyreachedbythetransmitter'ssignal.Figure1showsthesamedatapointswithtwodifferenttransmitterrotations.
Allinputcoordinatesareintegers(0-1000).Theradiusisapositiverealnumbergreaterthan0.Pointsontheboundaryofasemicircleareconsideredwithinthatsemicircle.Thereare1-150uniquepointstoexaminepertransmitter.Nopointsareatthesamelocationasthetransmitter.
Inputconsistsofinformationforoneormoreindependenttransmitterproblems.Eachproblembeginswithonelinecontainingthe(x,y)coordinatesofthetransmitterfollowedbythebroadcastradius,r.ThenextlinecontainsthenumberofpointsNonthegrid,followedbyNsetsof(x,y)coordinates,onesetperline.Theendoftheinputissignalledbyalinewithanegativeradius;the(x,y)valueswillbepresentbutindeterminate.Figures1and2representthedatainthefirsttwoexampledatasetsbelow,thoughtheyareondifferentscales.Figures1aand2showtransmitterrotationsthatresultinmaximalcoverage.
Foreachtransmitter,theoutputcontainsasinglelinewiththemaximumnumberofpointsthatcanbecontainedinsomesemicircle.
SampleInput
25253.5
7
2528
2327
2727
2423
2623
2429
2629
3502002.0
5
350202
350199
350198
348200
352200
99599510.0
4
10001000
999998
990992
1000999
100100-2.5
SampleOutput
3
4
4
Program2:Robots(TOJ_1926)
Yourcompanyprovidesrobotsthatcanbeusedtopickuplitterfromfieldsaftersportingeventsandconcerts.Beforerobotsareassignedtoajob,anaerialphotographofthefieldismarkedwithagrid.Eachlocationinthegridthatcontainsgarbageismarked.AllrobotsbeginintheNorthwestcornerandendtheirmovementintheSoutheastcorner.Arobotcanonlymoveintwodirections,eithertotheEastorSouth.Uponenteringacellthatcontainsgarbage,therobotwillpickitupbeforeproceeding.OncearobotreachesitsdestinationattheSoutheastcorneritcannotberepositionedorreused.Sinceyourexpensesaredirectlyproportionaltothenumberofrobotsusedforaparticularjob,youareinterestedinfindingtheminimumnumberofrobotsthatcancleanagivenfield.Forexample,considerthefieldmapshowninFigure1withrowsandcolumnsnumberedasshownandgarbagelocationsmarkedwitha'G'.Inthisscheme,allrobotswillbegininlocation1,1andendinlocation6,7.
Figure1-AFieldMap
Figure2belowshowstwopossiblesolutions,thesecondofwhichispreferablesinceitusestworobotsratherthanthree.
Figure2-TwoPossibleSolutions
Yourtaskistocreateaprogramthatwilldeterminetheminimumnumberofrobotsneededtopickupallthegarbagefromafield.
Aninputfileconsistsofoneormorefieldmapsfollowedbyalinecontaining-1-1tosignaltheendoftheinputdata.Afieldmapconsistsofoneormorelines,eachcontainingonegarbagelocation,followedbyalinecontaining00tosignaltheendofthemap.Eachgarbagelocationconsistsoftwointegers,therowandcolumn,separatedbyasinglespace.TherowsandcolumnsarenumberedasshowninFigure1.Thegarbagelocationswillbegiveninrow-majororder.Nosinglefieldmapwillhavemorethan24rowsand24columns.Thesampleinputbelowshowsaninputfilewithtwofieldmaps.ThefirstisthefieldmapfromFigure1.
Theoutputwillconsistofasinglelineforeachfieldmapcontainingtheminimumnumberofrobotsneededtocleanthecorrespondingfield.
SampleInput
12
14
24
26
44
47
66
00
11
22
44
00
-1-1
SampleOutput
2
1
Program3:ADPProblem(TOJ_1283)
Inthisproblem,youaretosolveaveryeasylinearequationwithonlyonevariablexwithnoparentheses!Anexampleofsuchequationsislikethefollowing:
2x-4+5x+300=98x
Anexpressioninitsgeneralform,willcontaina'='characterwithtwoexpressionsonitssides.Eachexpressionismadeupofoneormoretermscombinedby'+'or'-'operators.Nounaryplusorminusoperatorsareallowedintheexpressions.Eachtermiseitherasingleinteger,oranintegerfollowedbythelower-casecharacterxorthesinglecharacterxwhichisequivalentto1x.
Youaretowriteaprogramtofindthevalueofxthatsatisfiestheequation.Notethatitispossiblefortheequationtohavenosolutionorhaveinfinitelymany.Yourprogrammustdetectthesecasestoo.
Input
Thefirstnumberintheinputline,t(1≤t≤10)isthenumberoftestcases,followedbytlinesoflengthatmost255eachcontaininganequation.Thereisnoblankcharacterintheequationsandthevariableisalwaysrepresentedbythelower-casecharacterx.Thecoefficientsareintegersintherange(0...1000)inclusive.
Output
Theoutputcontainsonelinepertestcasecontainingthesolutionoftheequation.Ifsisthesolutiontotheequation,theoutputlineshouldcontain|_s_|(thefloorofs,i.e.,thelargestintegernumberlessthanorequaltos).Theoutputshouldbe"IMPOSSIBLE"or"IDENTITY"iftheequationhasnosolutionorhasinfinitesolutions,respectively.Notethattheoutputiscase-sensitive.
SampleInput
2
2x-4+5x+300=98x
x+2=2+x
SampleOutput
3
IDENTITY
Program4:Friends(TOJ_2469)Therearesomepeopletravelingtogether.Someofthemarefriends.Thefriendrelationistransitive,thatis,ifAandBarefriends,BandCarefriends,thenAandCwillbecomefriendstoo.Thesepeopleareplanningtobooksomeroomsinthehotel.Buteveryoneofthemdoesn'twanttolivewithstrangers,thatis,ifAandDarenotfriends,theycan'tliveinthesameroom.Giventheinformationaboutthesepeople,canyoudeterminehowmanyroomstheyhavetobookatleast?Youcanassumethattheroomsarelargeenough.InputThefirstlineoftheinputisthenumberoftestcases,andthensometestcasesfollowed.ThefirstlineofeachtestcasecontaintwointegersNandM,indicatingthenumberofpeopleandthenumberoftherelationshipbetweenthem.EachlineofthefollowingMlinescontaintwonumbersAandB(1≤A≤N,1≤B≤N,A≠B),indicatingthatAandBarefriends.Youcanassume1≤N≤100,0≤M≤N*(N-1)/2.Allthepeoplearenumberedfrom1toN.OutputOutputonelineforeachtestcase,indicatingtheminimumnumberofroomstheyhavetobook.SampleInput3531223455412233445100SampleOutput2110HintInthefirstsampletestcase,thereare5people.Weseethat1,2,3canliveinaroom,while4,5canliveinanotherroom.Sotheanswershouldbe2.Pleasenoteeventhough1and3arenot"direct"friends,buttheyareboth2'sfriends,so1and3arefriendstoo.Inthesecondsampletestcase,thereare5people,anytwoofthemwillbecomefriends.Sotheycanliveinoneroom.Inthethirdsampletestcase,thereare10peopleandnofriendrelationshipbetweenthem.Notwopeoplecanlivethesameroom.Theyhavetobook10rooms.group:CGROUPBProgram1:RobotinMaze(TOJ_2470)[code]Thereisarobottrappedinthemaze.Nowyouhavetosendoutsomeinstructions,tellingithowtoreachitsdestination.ThemazeisanM*Ngrid.Someofthecellsareempty,whileothersareoccupiedbythewall.Ofcoursetherobotcan'tmoveintothewall,andtherobotcan'tmoveoutsidethegridtoo.Therobotcanonlyacceptthreecommands:TURNLEFT,TURNRIGHTandGO.TherobotmayfacetoNorth,South,EastorWestduringthemovement.WhenitreceiveaTURNLEFTcommand,itwillrotate90degreetotheleft.Thatis,ifitfacestoeastbeforethecommand,itwillfacetonorthaftertheTURNLEFTcommand.TheTURNRIGHTcommandisalmostthesame,exceptthatthedirectionisopposite.WhenreceivetheGOcommand,therobotwillmove1unittowardsitsorientation,unlessthereisanonemptycellinfrontofit.Pleasenotetherobotisalwaysfacetonorthatthebeginning,i.e.,facetotheupperborderinthemazemap.(Themazemapwillbedescribedbelow.)Youwanttouseminimumnumberofinstructions,soyoushouldwriteaprogramforhelp.InputThefirstlineoftheinputisthenumberoftestcases.ThefirstlineofeachtestcasecontainstwointegersMandN,indicatingthesizeofthemaze.ThereareMlinesfollowed,eachlinecontainsexactlyNcharacters,describinganM*Nmaze.The'#'indicatingthewall,the'.'indicatingtheemptycell,the'S'and'T'indicatingthestartpointandthedestinationoftherobotrespectively.Therearenoothercharactersinthemazemap.Theorientationofthemazemapisjustthesameasthecommonsense.Thatis,theupper-leftcornerofthemazemapindicatingthenorth-westdirection,andthelower-rightcornerindicatingthesouth-east.Youcanassume1≤M≤100and1≤N≤100.Thereisonlyone'S'andone'T'inthemaze.OutputOutputonelineforeachtestcase,indicatingtheminimumnumberofinstructionsneeded.Oroutput-1ifit'simpossibletoreachtherobot'sdestination.SampleInput255######...##.#.##S#T######45#.#.##.#.##S#T######SampleOutput8-1HintThebestinstructionsequenceforthefirstsampletestcaseshouldbe:GO,GO,TURNRIGHT,GO,GO,TURNRIGHT,GO,GO.Andthelengthis8.
Object-orientedprogramdesign
Thisprojectisabouthowtoplaypoker,youneedtoimplementthreeclass:classCard,classDeck,classHand.
1、InclassCard,firstyoushoulddefinethekindofthecards.
enumRank{TWO=0,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING,ACE};
enumSuit{CLUBS=0,DIAMONDS,HEARTS,SPADES};
Theinsertionoperator<<isneededtobeoverloadedtoshowthekindofthecards.
friendostream&operator<<(ostream&,constCard&);
2、InclassDeck,firstyoumustconstructadeckwhichcontains52cards.TheDeck’sconstructorinitializesall52cardsinthedeck.
voidshuffle()istoshufflethe52cardsrandomly.
voiddeal(Hand&,unsigned=5);Thedeal()functioneverytimegetsthetopfivecardsofthedeckintothehand’scardsarray,thenitsortsthehand.
3、InclassHand,youshoulddeterminetheformofeveryfivecardsinhand.
voidsort();Thesort()functioniscalledbytheDeckclass.Itsortsfivecards,anditcanbeimplementedbyanysimplesortalgorithmsuchastheBubbleSort.
voiddisplay()usestheinsertionoperator<<thatisoverloadedintheCardclasstoshowthekindofthecards.
Thefollowingfunctionsdeterminetheformofeveryfivecards.
intisPair()istojudgewhethertherehasapaircards.
intisTwoPair()istojudgewhethertherehastwopairscards.
intisThreeOfKind()istojudgewhethertherehasthreesamerankcards.
intisFourOfKind()istojudgewhethertherehasfoursamerankcards.
intisStraight()istojudgewhethertherehasfivecardslike2,3,4,5,6or10,J,Q,K,Aandsoon.
intisFlush()istojudgewhethertherehasfivesamesuitcards.
intisFullHouse()istojudgewhethertherehasapairandthreesamerankcardslike2,2,3,3,3orJ,J,Q,Q,Qandsoon.
intisStraightFlush()istojudgewhetherthefivecardsarestraightandflush.
Theprojectistoshufflecardsrandomly,everytimeitgetsfivecardsfromthedecktohandthensortsthemandjudgestheformofthefivecards.Finallyitshowsfivecardsandtheirformonthescreen.Input‘y’fromkeyboardtoshuffleandshowagainorinput‘n’toendtheprogram.
[/code]
相关文章推荐
- 2017 程序设计实习之C++部分作业题汇总 - B:面向对象基础
- 2017 程序设计实习之C++部分作业题汇总 - F:模板 template
- 2017 程序设计实习之C++部分作业题汇总 - H:STL 容器与算法
- 2017 程序设计实习之C++部分作业题汇总 - C:运算符重载
- 2017 程序设计实习之C++部分作业题汇总 - D:继承与派生
- 2017 程序设计实习之C++部分作业题汇总 - E:多态与虚函数
- C++引用输出流作业
- c++作业2-分段函数求值,两点距离,模拟ATM
- C++第7次上机作业
- C++作业6
- 20170606_WPSC++实习_HR面
- c++上机作业求两点间距离
- c++上机作业测试体重是否标准并输出相关提示
- c++作业3
- 第六次C++上机作业
- c++作业3
- c++第3次作业—
- c++第三次作业-个人所得税
- 刚做的c++作业,高手可以帮忙指点下吗,谢谢啦!如有需要可加我 QQ:568835322
- c++作业6