位运算总结.docx

上传人:b****2 文档编号:1699767 上传时间:2023-05-01 格式:DOCX 页数:15 大小:95KB
下载 相关 举报
位运算总结.docx_第1页
第1页 / 共15页
位运算总结.docx_第2页
第2页 / 共15页
位运算总结.docx_第3页
第3页 / 共15页
位运算总结.docx_第4页
第4页 / 共15页
位运算总结.docx_第5页
第5页 / 共15页
位运算总结.docx_第6页
第6页 / 共15页
位运算总结.docx_第7页
第7页 / 共15页
位运算总结.docx_第8页
第8页 / 共15页
位运算总结.docx_第9页
第9页 / 共15页
位运算总结.docx_第10页
第10页 / 共15页
位运算总结.docx_第11页
第11页 / 共15页
位运算总结.docx_第12页
第12页 / 共15页
位运算总结.docx_第13页
第13页 / 共15页
位运算总结.docx_第14页
第14页 / 共15页
位运算总结.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

位运算总结.docx

《位运算总结.docx》由会员分享,可在线阅读,更多相关《位运算总结.docx(15页珍藏版)》请在冰点文库上搜索。

位运算总结.docx

位运算总结

六个位运算符:

&:

位与

|:

位或

^:

位异或

~:

取反

>>:

右位移

<<:

左位移

基本问题与对应表达式

1、去掉最后一位:

x>>1,101101->10110

2、最最后一位加0:

x<<1,101101->1011010

3、最最后一位加1:

x<<1+1,101101->1011011

4、把最后一位变成1:

x|1,101100->101101

5、把最后一位变成0:

x|1-1,101101->101100

6、最后一位取反:

x^1,101100->101101,101101->101100

7、把右数第k位变成1:

x|(1<<(k-1))

8、把右数第k位变成0:

x&~(1<<(k-1))

9、把右数第k位取反:

x^(1<<(k-1))

10、取末k位:

x&(1<

11、把右边连续的1变0:

x&(x+1),100101111->100100000

12、把右边第一个0变成1:

x|(x+1),100101111->100111111

13、把右边第一个1变成0:

x&(x-1),10010100->10010000

14、把右边连续的0变成1:

x|(x-1),11011000->11011111

15、取右边连续的1:

(x^(x+1))>>1,100101111->1111

16、去掉右边第一个1的左边:

x&(x^(x-1)),100101000->1000

17、判断奇偶:

x&1

王特:

1324

HoledoxMoving

Description

Duringwinter,themosthungryandseveretime,Holedoxsleepsinitslair.Whenspringcomes,Holedoxwakesup,movestotheexitofitslair,comesout,andbeginsitsnewlife.

Holedoxisaspecialsnake,butitsbodyisnotverylong.Itslairislikeamazeandcanbeimaginedasarectanglewithn*msquares.Eachsquareiseitherastoneoravacantplace,andonlyvacantplacesallowHoledoxtomovein.Usingorderedpairofrowandcolumnnumberofthelair,thesquareofexitlocatedat(1,1).

Holedox'sbody,whoselengthisL,canberepresentedblockbyblock.AndletB1(r1,c1)B2(r2,c2)..BL(rL,cL)denoteitsLlengthbody,whereBiisadjacenttoBi+1inthelairfor1<=i<=L-1,andB1isitshead,BLisitstail.

Tomoveinthelair,Holedoxchoosesanadjacentvacantsquareofitshead,whichisneitherastonenoroccupiedbyitsbody.Thenitmovestheheadintothevacantsquare,andatthesametime,eachotherblockofitsbodyismovedintothesquareoccupiedbythecorrespondingpreviousblock.

Forexample,intheFigure2,atthebeginningthebodyofHoledoxcanberepresentedasB1(4,1)B2(4,2)B3(3,2)B4(3,1).Duringthenextstep,observingthatB1'(5,1)istheonlysquarethattheheadcanbemovedinto,HoledoxmovesitsheadintoB1'(5,1),thenmovesB2intoB1,B3intoB2,andB4intoB3.Thusafteronestep,thebodyofHoledoxlocatesinB1(5,1)B2(4,1)B3(4,2)B4(3,2)(seetheFigure3).

GiventhemapofthelairandtheoriginallocationofeachblockofHoledox'sbody,yourtaskistowriteaprogramtotelltheminimalnumberofstepsthatHoledoxhastotaketomoveitsheadtoreachthesquareofexit(1,1).

Input

Theinputconsistsofseveraltestcases.Thefirstlineofeachcasecontainsthreeintegersn,m(1<=n,m<=20)andL(2<=L<=8),representingthenumberofrowsinthelair,thenumberofcolumnsinthelairandthebodylengthofHoledox,respectively.ThenextLlinescontainapairofrowandcolumnnumbereach,indicatingtheoriginalpositionofeachblockofHoledox'sbody,fromB1(r1,c1)toBL(rL,cL)orderly,where1<=ri<=n,and1<=ci<=m,1<=i<=L.ThenextlinecontainsanintegerK,representingthenumberofsquaresofstonesinthelair.ThefollowingKlinescontainapairofrowandcolumnnumbereach,indicatingthelocationofeachsquareofstone.Thenablanklinefollowstoseparatethecases.

Theinputisterminatedbyalinewiththreezeros.

Note:

BiisalwaysadjacenttoBi+1(1<=i<=L-1)andexitsquare(1,1)willneverbeastone.

Output

ForeachtestcaseoutputonelinecontainingthetestcasenumberfollowedbytheminimalnumberofstepsHoledoxhastotake."-1"meansnosolutionforthatcase.

SampleInput

564

41

42

32

31

3

23

33

34

444

23

13

14

24

4

21

22

34

42

000

SampleOutput

Case1:

9

Case2:

-1

胡化藤:

3460

Booksort

Description

TheLeidenUniversityLibraryhasmillionsofbooks.Whenastudentwantstoborrowacertainbook,heusuallysubmitsanonlineloanform.Ifthebookisavailable,thenthenextdaythestudentcangoandgetitattheloancounter.Thisisthemodernwayofborrowingbooksatthelibrary.

Thereisonedepartmentinthelibrary,fullofbookcases,wherestilltheoldwayofborrowingisinuse.Studentscansimplywalkaroundthere,pickoutthebookstheylikeand,afterregistration,takethemhomeforatmostthreeweeks.

Quiteoften,however,ithappensthatastudenttakesabookfromtheshelf,takesacloserlookatit,decidesthathedoesnotwanttoreadit,andputsitback.Unfortunately,notallstudentsareverycarefulwiththislaststep.Althougheachbookhasauniqueidentificationcode,bywhichthebooksaresortedinthebookcase,somestudentsputbackthebookstheyhaveconsideredatthewrongplace.Theydoputitbackontotherightshelf.However,notattherightpositionontheshelf.

Otherstudentsusetheuniqueidentificationcode(whichtheycanfindinanonlinecatalogue)tofindthebookstheywanttoborrow.Forthem,itisimportantthatthebooksarereallysortedonthiscode.Alsoforthelibrarian,itisimportantthatthebooksaresorted.Itmakesitmucheasiertocheckifperhapssomebooksarestolen:

notborrowed,butyetmissing.

Therefore,everyweek,thelibrarianmakesaroundthroughthedepartmentandsortsthebooksoneveryshelf.Sortingoneshelfisdoable,butstillquitesomework.Thelibrarianhasconsideredseveralalgorithmsforit,anddecidedthattheeasiestwayforhimtosortthebooksonashelf,isbysortingbytranspositions:

aslongasthebooksarenotsorted,

takeoutablockofbooks(anumberofbooksstandingnexttoeachother),

shiftanotherblockofbooksfromtheleftortherightoftheresulting‘hole’,intothishole,

andputbackthefirstblockofbooksintotheholeleftopenbythesecondblock.

Onesuchsequenceofstepsiscalledatransposition.

Thefollowingpicturemayclarifythestepsofthealgorithm,whereXdenotesthefirstblockofbooks,andYdenotesthesecondblock.

Ofcourse,thelibrarianwantstominimizetheworkhehastodo.Thatis,foreverybookshelf,hewantstominimizethenumberoftranspositionshemustcarryouttosortthebooks.Inparticular,hewantstoknowifthebooksontheshelfcanbesortedbyatmost4transpositions.Canyoutellhim?

Input

Thefirstlineoftheinputfilecontainsasinglenumber:

thenumberoftestcasestofollow.Eachtestcasehasthefollowingformat:

Onelinewithoneintegernwith1≤n≤15:

thenumberofbooksonacertainshelf.

Onelinewiththenintegers1,2,…,ninsomeorder,separatedbysinglespaces:

theuniqueidentificationcodesofthenbooksintheircurrentorderontheshelf.

Output

Foreverytestcaseintheinputfile,theoutputshouldcontainasingleline,containing:

iftheminimalnumberoftranspositionstosortthebooksontheiruniqueidentificationcodes(inincreasingorder)isT≤4,thenthisminimalnumberT;

ifatleast5transpositionsareneededtosortthebooks,thenthemessage"5ormore".

SampleInput

3

6

134625

5

54321

10

68534729110

SampleOutput

2

3

5ormore

乐思文:

1079

Ratio

Description

Ifyoueverseeatelevisedreportonstockmarketactivity,you'llheartheanchorpersonsaysomethinglike``Gainersoutnumberedlosers14to9,''whichmeansthatforevery14stocksthatincreasedinvaluethatday,approximately9otherstocksdeclinedinvalue.Often,asyouhearthat,you'llseeonthescreensomethinglikethis:

Gainers1498

Losers902

Asapersonwithaheadfornumbers,you'llnoticethattheanchorpersoncouldhavesaid``Gainersoutnumberedlosers5to3'',whichisamoreaccurateapproximationtowhatreallyhappened.Afterall,theexactratioofwinnerstolosersis(tothenearestmillionth)1.660754,andhereportedaratioof14to9,whichis1.555555,foranerrorof0.105199;hecouldhavesaid``5to3'',andintroducedanerrorofonly1.666667-1.660754=0.005913.Theestimate``5to3''isnotasaccurateas``1498to902''ofcourse;evidently,anothergoalistousesmallintegerstoexpresstheratio.So,whydidtheanchorpersonsay``14to9?

''Becausehisalgorithmistolopoffthelasttwodigitsofeachnumberandusethoseastheapproximateratio.

Whattheanchormanneedsisalistofrationalapproximationsofincreasingaccuracy,sothathecanpickonetoreadontheair.Specifically,heneedsasequence{a_1,a_2,...,a_n}wherea_1isarationalnumberwithdenominator1thatmostexactlymatchesthetrueratioofwinnerstolosers(roundingupincaseofties),a_{i+1}istherationalnumberwithleastdenominatorthatprovidesamoreaccurateapproximationthana_i,anda_nistheexactratio,expressedwiththeleastpossibledenominator.Giventhissequence,theanchorpersoncandecidewhichratiogivesthebesttradeoffbetweenaccuracyandsimplicity.

Forexample,if5stocksroseinpriceand4fell,thebestapproximationwithdenominator1is1/1;thatis,foreverystockthatfell,aboutonerose.Thisanswerdiffersfromtheexactanswerby0.25(1.0vs1.25).Thebestapproximationswithtwointhedenominatorare2/2and3/2,butneitherisanimprovementontheratio1/1,soneitherwouldbeconsidered.Thebestapproximationwiththreeinthedenominator4/3,ismoreaccuratethananyseensofar,soitisonethatshouldbereported.Finally,ofcourse,5/4isexactlytheratio,andsoitisthelastnumberreportedinthesequence.

Canyouautomatethisprocessandhelptheanchorpeople?

Input

inputcontainsseveralpairsofpositiveintegers.Eachpairisonalinebyitself,beginninginthefirstcolumnandwithaspacebetweenthetwonumbers.Thefirstnumberofapairisthenumberofgainingstocksfortheday,andthesecondnumberisthenumberoflosingstocksfortheday.Thetotalnumberofstocksneverexceeds5000.

Output

Foreachinputpair,thestandardoutputshouldcontainaseriesofapproximationstotheratioofgainerstolosers.Thefirstapproximationhas'1'asdenominator,andthelastisexactlytheratioof

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2