您的位置:首页 > 编程语言 > C语言/C++

c++实习的作业

2008-03-16 16:50 323 查看
大一上学期学了c++,下学期要实习,而实习题目就是acm(toj)上的9道题,附加一个纸牌游戏的类

网址:http://acm.tju.edu.cn

Program1WebNavigationTOJ_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]
Input

Inputisasequenceofcommands.ThecommandkeywordsBACK,FORWARD,VISIT,andQUITareallinuppercase.URLshavenowhitespaceandhaveatmost70characters.Youmayassumethatnoprobleminstancerequiresmorethan100elementsineachstackatanytime.TheendofinputisindicatedbytheQUITcommand.

Output

ForeachcommandotherthanQUIT,printtheURLofthecurrentpageafterthecommandisexecutedifthecommandisnotignored.Otherwise,print"Ignored".Theoutputforeachcommandshouldbeprintedonitsownline.NooutputisproducedfortheQUITcommand.

SampleInput

VISIT'target='_blank'>http://acm.ashland.edu/[/code]
VISIT'target='_blank'>http://acm.baylor.edu/acmicpc/[/code]
BACK

BACK

BACK

FORWARD

VISIT'target='_blank'>http://www.ibm.com/[/code]
BACK

BACK

FORWARD

FORWARD

FORWARD

QUIT

SampleOutput

http://acm.ashland.edu/

http://acm.baylor.edu/acmicpc/

http://acm.ashland.edu/

http://www.acm.org/

Ignored

http://acm.ashland.edu/

http://www.ibm.com/

http://acm.ashland.edu/

http://www.acm.org/

http://acm.ashland.edu/

http://www.ibm.com/

Ignored



Program2OctalFractions(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.



SAMPLEINPUT

0.75

0.0001

0.01234567



SAMPLEOUTPUT

0.75[8]=0.953125[10]

0.0001[8]=0.000244140625[10]

0.01234567[8]=0.020408093929290771484375[10]


Program3Soundex(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

Program4Psuedo-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

Program1TransmittersTOJ_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

Program2Robots(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

Program3ADPProblem(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

Program4Friends(TOJ_2469)
Therearesomepeopletravelingtogether.Someofthemarefriends.Thefriendrelationistransitive,thatis,ifAandBarefriends,BandCarefriends,thenAandCwillbecomefriendstoo.

Thesepeopleareplanningtobooksomeroomsinthehotel.Buteveryoneofthemdoesn'twanttolivewithstrangers,thatis,ifAandDarenotfriends,theycan'tliveinthesameroom.

Giventheinformationaboutthesepeople,canyoudeterminehowmanyroomstheyhavetobookatleast?Youcanassumethattheroomsarelargeenough.

Input

Thefirstlineoftheinputisthenumberoftestcases,andthensometestcasesfollowed.

ThefirstlineofeachtestcasecontaintwointegersNandM,indicatingthenumberofpeopleandthenumberoftherelationshipbetweenthem.EachlineofthefollowingMlinescontaintwonumbersAandB(1≤A≤N,1≤B≤N,A≠B),indicatingthatAandBarefriends.

Youcanassume1≤N≤100,0≤M≤N*(N-1)/2.Allthepeoplearenumberedfrom1toN.

Output

Outputonelineforeachtestcase,indicatingtheminimumnumberofroomstheyhavetobook.

SampleInput

3

53

12

23

45

54

12

23

34

45

100

SampleOutput

2

1

10

Hint

Inthefirstsampletestcase,thereare5people.Weseethat1,2,3canliveinaroom,while4,5canliveinanotherroom.Sotheanswershouldbe2.Pleasenoteeventhough1and3arenot"direct"friends,buttheyareboth2'sfriends,so1and3arefriendstoo.

Inthesecondsampletestcase,thereare5people,anytwoofthemwillbecomefriends.Sotheycanliveinoneroom.

Inthethirdsampletestcase,thereare10peopleandnofriendrelationshipbetweenthem.Notwopeoplecanlivethesameroom.Theyhavetobook10rooms.


group:C

GROUPBProgram1RobotinMazeTOJ_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.


Input


Thefirstlineoftheinputisthenumberoftestcases.

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.


Output


Outputonelineforeachtestcase,indicatingtheminimumnumberofinstructionsneeded.Oroutput-1ifit'simpossibletoreachtherobot'sdestination.


SampleInput

2

55

#####

#...#

#.#.#

#S#T#

#####

45

#.#.#

#.#.#

#S#T#

#####


SampleOutput

8

-1


Hint

Thebestinstructionsequenceforthefirstsampletestcaseshouldbe: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]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: