收藏 分销(赏)

acm算法经典例题.doc

上传人:1587****927 文档编号:1669086 上传时间:2024-05-07 格式:DOC 页数:77 大小:412KB
下载 相关 举报
acm算法经典例题.doc_第1页
第1页 / 共77页
acm算法经典例题.doc_第2页
第2页 / 共77页
acm算法经典例题.doc_第3页
第3页 / 共77页
acm算法经典例题.doc_第4页
第4页 / 共77页
acm算法经典例题.doc_第5页
第5页 / 共77页
点击查看更多>>
资源描述

1、一、数论1: Wolf ad Rabbt描述There s a ll with holes arnd、 The holes arese fom o -1、A abbt musthide inoneof ehole、 A lf eaches theabbtinantclockwise ore、 efirst hol he get nto i the one igned with 0、Te h il get nto th oe evm oes、For exmple, 2and n6, thwolfwill gtit hoes which e ged 0,2,4,0、 I th rbbi ide i

2、the ole which signed ,or ,se wlsurvive、So we call tes holes hesafe hols、输入Theinut sta wh a psitivintegr P whcict the number oftst aes、Tn the folwin P lines,each lin cosis 2 psitive nter m andn(0m,21474868)、输出oreach input m, ifafe hos exs, ououd outt ES, ls utput NO in a se line、样例输入21 22 样例输出NOYES翻译

3、:描述一座山有个洞,洞被标记为从0到-1。兔子必须藏在一个洞中。狼会逆时针方向搜索兔子。狼一开始在洞0,然后她会每m个洞进去一次。例如:m2,n=,狼就会依次进入洞0 2 0。如果兔子藏在 就安全。输入第一行一个数字p,表示下面有p组测试数据,每组测试数据个数m (0,ung namespce std;log longgatstonDiviso(on ong a, long lng b)/最大公约数 ng lon t; whie(b) = a b; = b; b t; etr;int ain() inti, ; long long m,n; cin p; for( 0;i n; if(gate

4、stmonDivior(m,n) = 1)/公约数为1说明互斥,没有安全洞穴 cu Nn; es cou YESn; eturn ;2: a描述给定a与b,输出a得最后一个数字。输入输入数据有多组,每组数据占一行,每行为a与b得值(0a,b=3)输出对每组输入数据,输出ab得最后一位数字,每组数据占一行。样例输入2 23 4样例输出1思路/方法:如果您模拟a次肯定会超时,而且数字还会超出Int范围题目说只要最后一个数字,回想一下小学时学得乘法过程,貌似乘数最后一位与前面得无关我们大胆得尝试一下用a得个位代替a,然后我们发现循环次还就是会超时,这就是我们要想办法减少循环得次数,试一下就是不就是有

5、周期规律这时我们来列举一下2得n次方得个位: 8 6 2 4 8 6我们发现周期为4,我们在试试1-9得n次方,发现周期都就是4,所以,我们可以用b4代替b,需要注意得就是,当b=时,我们需要给她加上4,不然就不循环了。代码:includsdio、it mn() in a, b, i, t; ile(anf(%dd, &a, &b) != EOF) b b 4; f( = ) b = ; = a 10; t = 1; for(i = 0;i b; i+) = t a; t =t% 0; prnt(dn,t); reurn 0;3: 筛选法求素数描述请使用筛选法输出a, b之间得所有素数。输入输

6、入数据有多组,每组数据占一行,每行2个正整数a与b,其中2=a=b=100000。输出每组数据按从小到大得顺序输出a, b中所有得素数,每行最多输出0个素数。每组数据之后空一行。样例输入2 3 50样例输出 32 5 13 17 1923 2937 1 3思路方法:这题测试得数据量很大,所以尽量少循环,尽量少判断,要非常精简才能通过。、定义一个全局变量shor s1000001;/全局变量默认为2、把数组中下标为奇数得值改为1,偶数不用改,因为除了,其她偶数都不就是素数s2 = 1;/2也就是素数fr(= ; i 100; i = i + 2)/把奇数全部假设为素数 si 1;、把素数得倍数挖

7、掉,改为。(从2开始到根号000000之间得素数得倍数挖掉)fo(i2;i 000; +)/小于根号1000000 i(i)/判断就是否为素数,只有素数得倍数才挖掉 fr( = i *2;j 0001; j=j + )/把得倍数得值改成0 sj = 0;、最后一点就是输出格式,每组之间一个空行,另外一行最多10个。定义一个变量来记录输出了多少个,达到十个就输出换行。具体瞧代码。代码:#ncleshorts00001;/全局变量默认为inma() tt, a, b,; s2 1;/也就是素数 fr(i = ; 0001; =i + 2)/把奇数全部假设为素数 si= 1; fo(i =2; i

8、1000; i+)/小于根号0000 f(si) (j i 2; j 000; = j i)/把得倍数得值改成0 sj= ; wle(nf(%d %d, , b) !=EOF) t = 0; o(i =a; =b; i+) if(si)/素数就输出 if(t) if(t = 10) pritf(n); 0; els prtf(); +; pntf(%d, i); prtf(nn); etur ;4: h onesto emain描述hre re sders standing n o lin、 hy a marke fro t , o righ tlef、An they are ien aumb

9、er m、 heth lsnumeredf, sraigtfom terigh-hnman、Th oe whorportea nbertha the mutple of m akept i the ine、 Oters hae to leve the lie、 They conudng thitillth number opeopl h lin s ls tan m、 For example, ifthee are 10 soldir,and = 3、 Fr thir time te soldiersh ae ard 3, 6, 9 emn in th lne、 For te second e

10、 the sldier whosmarked rmai line、 Because te mber slders i the lnislsshan m, ohe sldier arked 9 wasthe onlyone to rainin h ine、 ow e wantto know whoi be t nes to eain, can ou tell s?输入Tere aeseveral tet casin he ipt、Eaches cass is y nelie,ontintwo integs nad m、(3 = =109,2 = m = n)、Thnput ends wen =

11、and m = 0、输出For ea tetcas, outpu tolins、 The first line tainsoeinegr,t nmbr fsodiers to rin、 Thsecond line onais x nees, the numersarkedn the oldiersho remin n he i、 You shoud oput them in icresin order、样例输入10 8 30 0样例输出1923 6翻译:描述有个士兵站在一行。她们被从右到左标记为1到N。她们被给与了一个数字m。然后士兵直接从右面报数。报得数就是得倍数得留下来,其她人离开。然后继

12、续上述操作,直到人数少于m。例如,有10个士兵,m=3。第一次士兵报数为3 69得留下,第二次士兵报数为9得留下。输入有多组测试数据。每组一行两个数n m(3 = n = 109, m = n),以0 0结束输出每组输出两行,第一行输出一个表示留下来得士兵数量,第二行输出个留下来得士兵得编号。思路/方法:这题用数组来存储士兵状态就会超时,所以我们需要更精简得算法,很明显可以瞧出这就是道数学题,所以我们多举几个例子,瞧瞧就是否有规律。m=2时 2 221 23424 3 4 624641 3 67 82 6 84 88m时12 31 2 3431 2 3 4 5 63 61 24 5 6 8 9

13、 699由上面得几个例子可以瞧出关键就是找到一个不大于得最大得mx。比如m=2得时候,依次就是21 2 22 223,当一样时,她们得结果值一样,并且就就是mx。当m=3时,依次就是31 31 332,不难发现,x一样时,结果得第一个数一样就是mx。接下来要找出有多少个结果值,比较m=3时得前3组数据,发现第三组第二个结果6就是*2且不大于n=6,我们大胆推断m得个数就就是不大于n得得倍数得个数。然后我们继续举个例子检验一下推论。1 2 4 6 7 8 9 11 1 8 12如上,当m=时,只有m1不大于n,所以结果第一个数为,然后后面有8 12为得倍数,且不大于n,所以得到3个结果,与例子得

14、结果一致。这样就成功推出了解题方法,虽然不严谨,但作为一般人,能做出来就行了。代码:#incdeung nmespace std;int mai() oglog m, n, eai, id, j;/i表示下标,j表示当前第几个报数 le(cn n m, !(n = 0 & m= 0)) /得到不大于得 mx 得值 j 1; for(id= 1; i+) =j ; if( * m n) brek; rmin = 0; f(id = 1;id+) if(j * i n)/得到不大于n得 m得倍数 得个数 ain+; else re; outremain endl; for(id =1;d rema

15、n; id+) ct id * ; cout * #nlude strig、hon og reestmoDiisor(long lng , long log) log l; while(b) t = a b; = ; b=t; retr a;nt ain() t i, , ,k; lo log demintor, numrator, divsor, cycica, oClic;/分母分子,非循环部分与循环部分 char a20,nonCycliclString2, yclirg20;/必须用文本,否则会丢失位数,比如0、000这样得会把0丢了 saf(%d , &n); o(i = 0; i

16、; +) scanf(、%, ); getchar(); f(trchr(a, () != NULL)/有循环小数得情况 i(a0 ()如果就是纯循环小数 sscanf(a, (%s, cclcaSting);/只能这样读取,把括号补充完整也一样得结果 clclStringstrlen(cyclcatring)-1 = 0;/手动删除后面得括号。读取到了循环部分 scn(cclalSg ,I64, &cyclcal); nonCyclicaStrig0 = 0; numa= cclil;/分子就等于循环节 else for(j 0;j !(;j+)/读取到(就停止。读取非循环部分 nonCic

17、alStri = j; nonyclicaStringj 0; or(k = 0, j 1;aj ! ); j+, +)/从(右边一个开始读取到)就停止。读取循环部分 cylicalStingk aj; cycicaStrigk = 0; sscanf(nnCyclicalString, %I6d,&nonCyclial);把非循环部分得值放入到变量中 scanf(ccliarin, %I64d, &clicl);/把循环部分得值放入到变量中 umeraor = nonclial;/把分子得值先变成非循环部分 r(j = 0; clicaltingj != 0; j+)/往分子尾部加入循环节部

18、分 nuerat nmrator * 10+ (cycialStringj-0); nurto =nraor- onCyclical;/非循环部分与循环部分得组合 非循环部分 /计算分母 eniao=; for(j = 0; cyllStrigj!= 0;j)/加上循环节个数个9 deninat = denomnator 10 + 9; for(j =; nonCylicalStringj !=; j+)/加上非循环部分个0 deoiator = enmtr* ; dvio = greatesmonivsor(nrator, dnominator); print(%6d/%64d,numera

19、tor divisor, dnoinat / divisor); lse/非循环小数 ssanf(,%I4d, umertr);/把小数部分存到变量中 dnotor = ; fr(j = 0; != ; j+)/计算分母 deoitor =eomiator 10; divis = geatstmonDivior(numeator, denomnaor); print(I4d/%I64d, umerat iisor, deomnator ivisor); turn 0;6: 全排列描述任意输入个不重复得整数序列,输出序列得全排列。输入测试数据有多组,第一行就是整数t(020),代表测试组数。每组

20、测试数据有两行,第一行就是整数得个数n(n),第二行就是个不重复得整数。输出按递增得顺序输出序列得全排列。每个测试数据后面输出一个空行。样例输入31 5样例输出1 355 33 153 5 15 1 353 1思路/方法:全排列有递归与非递归算法,具体网上有,这里我们为了代码简洁,采用S里面得函数来实现,容易理解,而且好写。首先引用lgoithm这个库,然后里面有ort排序函数与nexpermuttion下一个排列函数。sort升序排序,参数分别就是首地址与末地址nextperutation就是判断就是否有下一个排列,有返回true,并且改变数组状态,否则返回fase。参数分别就是首地址与末地

21、址代码:#include nclue algoritmusing namspacestd;vd disla(int , int n) ii; fo( = 0;i n ; i+) o ; ut ; for(i 0; i ; n an; or(j = 0; j n;+) cin aj; fulrutation(, n); cou endl; turn0;: (1+)n描述Pease calculae th coefficien mdul 2 fx n(1+)、输入For ech case,there ar two intges,i (0=i=2311)输出Feahse, printthe ceff

22、icnt modulo 2 of i in (1+x)nona snle line、样例输入 12样例输出0翻译:描述请计算(1)中xi得系数对2取模得值输入每组测试数据有两个整数 i (0=i=n=231-)输出每组输出一行,即对2取模得值思路/方法:(1 + )n展开后i项得系数得对2取余即求 c(,)xi中得c(n, i) % 2得值如果我们计算组合数在对2取余,就会超时,所以我们需要更简便得算法既然就是对2取模,就就是奇偶性咯,所以我们需要一个组合数得奇偶性判断得算法对于组合数(, ) 如果(n&)=m,那么该组合数就是奇数,否则为偶数代码:#icude stdio、it n () o

23、ng long n, i; whie(scan(I64 %6d, n, &i) != EOF) if((ni)= i)/奇数,输出 prntf(1); ese pnt(0n); reurn 0;8: Smmin divisrs描述Inh 18t cenry, L、 Eur inveed functin he calledsm to stuy prortiesof whole nubers、 He wasinteested n arin a psitie numbr tthe smis psitive vso、 In thi prle we xten Eulersfuncton o ratin、

24、iena potive raionalnume (i、e、,afrcton) in iesttems a/b, we dfne S(a/b) tbeth sf allpositive ers ofthe frm x/ whr x ia positive diviso o aan is poitive dr f b、 r examle,thepostive divsors f 4ar 1,2and ndth poiive divsors f 3 are ad , o S(4/) 11 + 2/ + 41 + 1/3+ /3 +/ = 28/3、输入Eac le of ipu wi be o th

25、e form a/ (wth n whit space) where ad b re nt in e rge from 1 t 16000、输出Each lie ofotut is of te fom a/b we ad a ntger, ad he actin i in simpet erms (so 1/2 wilbe outut instedof /4for xampl)、样例输入6/12/10/49样例输出114/11767/7翻译:描述在18世纪,L、 uer发明了一个功能去研究数字得特性,她称之为sgma。她对比较整数得正因子得与感兴趣。在这个问题我们扩展uler得功能到分数。给一

26、个正分数最简形式/b,我们定义S(/) 就是所有整数x/得与,/就是正因子组成,x来自a得因子,y来自得因子。例如,4得正因子就是12 4;3得正因子就是1 3。所以(4/3) = 11 + 2/1 + /1 3+ 3 + 43= 2/。输入每行输入/b,a b范围1到16000、输出每行输出(a/b)得值,分数最简形式。思路/方法:1、定义数组a与b分别存储输入得两个数得因子。2、定义一个结构体用来表示分数3、定义一个函数用来计算分数相加4、把a与因子组合起来得分数累加起来,然后约分为最简。不太好说,具体瞧下面代码,不难。代码:#incueyedef stct rtion/分数 it num

27、ertor;/分子 int demnor;/分母frain;ntgeatestmonDiviso(int a,t b) n t; wle(b) t =a% ; a = b; b =t; rturna;fractinddFron(fction * f1, frcio * f2) acton s; intdivso; if(1-deninator = f-dnomito| 1-nerator= )两数分母相同 s、numerto = f1-urtr +f2-nueaor;/分子相加 s、denmina f2dnomiator; ele s、denoiato= f1-deoiato* fdemintor;/分母通分 s、numeratr = f1-nmet* f-enoinator + f2nmerato * f1-denoiaor;/分子通分后相加 iviso = reesmonDivs(s、merto, s、nomatr);/求最大公约数 /约分 s、nuerator /= divso; s、enominat /= dvisor; retrs;nt ain() int a16001, b16001,i, j, ca, cb; factin s, t; hile(caf(d/%, a,b)!= OF)把a与b存到 b0

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服