ACM pku 1140 解题报告(Expanding Fractions )
2006-02-16 14:07
281 查看
ExpandingFractions
TimeLimit:1000MSMemoryLimit:10000K
TotalSubmit:555Accepted:192
Description
Inthisproblemyouaretoprintthedecimalexpansionofaquotientoftwointegers.Asyouwellknow,thedecimalexpansionsofmanyintegerquotientsresultindecimalexpansionswithrepeatingsequencesofdigits.Youmustidentifythese.Youwillprintthedecimalexpansionoftheintegerquotientgiven,stoppingjustastheexpansionterminatesorjustastherepeatingpatternistorepeatitselfforthefirsttime.Ifthereisarepeatingpattern,youwillsayhowmanyofthedigitsareintherepeatingpattern.
Input
Therewillbemultipleinputinstances,eachinstanceconsistsoftwopositiveintegersonaline.Thefirstintegerrepresentsthenumeratorofthefractionandthesecondrepresentsthedenominator.Inthisproblem,thenumeratorwillalwaysbelessthanthedenominatorandthedenominatorwillbelessthan1000.Inputterminateswhennumeratoranddenominatorarebothzero.
Output
Foreachinputinstance,theoutputshouldconsistofthedecimalexpansionofthefraction,startingwiththedecimalpoint.Iftheexpansionterminates,youshouldprintthecompletedecimalexpansion.Iftheexpansionisinfinite,youshouldprintthedecimalexpansionupto,butnotincludingthedigitwheretherepeatedpatternfirstrepeatsitself.
Forinstance,4/11=.3636363636...,shouldbeprintedas.36.(Notethattheshortestrepeatingpatternshouldbefound.Intheaboveexample,3636and363636,amongothers,arerepeatingpatterns,buttheshortestrepeatingpatternis36.)
Sincesomeoftheseexpansionsmaybequitelong,multiplelineexpansionsshouldeachcontainexactly50charactersoneachline(exceptthelastline,which,ofcourse,maybeshorter)-thatincludesthebeginningdecimalpoint.
Onthelineimmediatelyfollowingthelastlineofthedecimalexpansionthereshouldbealinesayingeither``Thisexpansionterminates.",or``Thelastndigitsrepeatforever.",wherenisthenumberofdigitsintherepeatingpattern.
Helpfulhint:Thenumberofdigitsbeforethepatternisrepeatedwillneverbemorethanthevalueofthedenominator.
SampleInput
SampleOutput
.428571
Thelast6digitsrepeatforever.
.43125
Thisexpansionterminates.
.113
Thelast2digitsrepeatforever.
.4344262295081967213114754098360655737704918032786
885245901639
Thelast60digitsrepeatforever.
Source
EastCentralNorthAmerica1994
真分式除式的原理就是用一个数乘以除数,使被除数被足0后与得到的这个积的差小于除数.然后再用这个差补足0再重复上面的步骤.
判断从那开始循环时,大家是不是会很快地想到用类似字符比较的方法?其实,只要看是不是有和前面得出来的余数相同的余数就可以了!因为余数是会马上被当作被除数来用的,而相同的除数不变,被除数又与前面的相同,自然除得的结果会与前面的重复.太奇妙了~
if((i==49)||((i>51)&&((i+1)%50==0)))printf("/n");/*关键是这句!可以看出,第一行只有49个数字(因为包括了“.”),而第二行以后就要有50个*/
这个错可找死我了,正是练习了发现问题的能力~
TimeLimit:1000MSMemoryLimit:10000K
TotalSubmit:555Accepted:192
Description
Inthisproblemyouaretoprintthedecimalexpansionofaquotientoftwointegers.Asyouwellknow,thedecimalexpansionsofmanyintegerquotientsresultindecimalexpansionswithrepeatingsequencesofdigits.Youmustidentifythese.Youwillprintthedecimalexpansionoftheintegerquotientgiven,stoppingjustastheexpansionterminatesorjustastherepeatingpatternistorepeatitselfforthefirsttime.Ifthereisarepeatingpattern,youwillsayhowmanyofthedigitsareintherepeatingpattern.
Input
Therewillbemultipleinputinstances,eachinstanceconsistsoftwopositiveintegersonaline.Thefirstintegerrepresentsthenumeratorofthefractionandthesecondrepresentsthedenominator.Inthisproblem,thenumeratorwillalwaysbelessthanthedenominatorandthedenominatorwillbelessthan1000.Inputterminateswhennumeratoranddenominatorarebothzero.
Output
Foreachinputinstance,theoutputshouldconsistofthedecimalexpansionofthefraction,startingwiththedecimalpoint.Iftheexpansionterminates,youshouldprintthecompletedecimalexpansion.Iftheexpansionisinfinite,youshouldprintthedecimalexpansionupto,butnotincludingthedigitwheretherepeatedpatternfirstrepeatsitself.
Forinstance,4/11=.3636363636...,shouldbeprintedas.36.(Notethattheshortestrepeatingpatternshouldbefound.Intheaboveexample,3636and363636,amongothers,arerepeatingpatterns,buttheshortestrepeatingpatternis36.)
Sincesomeoftheseexpansionsmaybequitelong,multiplelineexpansionsshouldeachcontainexactly50charactersoneachline(exceptthelastline,which,ofcourse,maybeshorter)-thatincludesthebeginningdecimalpoint.
Onthelineimmediatelyfollowingthelastlineofthedecimalexpansionthereshouldbealinesayingeither``Thisexpansionterminates.",or``Thelastndigitsrepeatforever.",wherenisthenumberofdigitsintherepeatingpattern.
Helpfulhint:Thenumberofdigitsbeforethepatternisrepeatedwillneverbemorethanthevalueofthedenominator.
SampleInput
37 345800 112990 53122 00
SampleOutput
.428571
Thelast6digitsrepeatforever.
.43125
Thisexpansionterminates.
.113
Thelast2digitsrepeatforever.
.4344262295081967213114754098360655737704918032786
885245901639
Thelast60digitsrepeatforever.
Source
EastCentralNorthAmerica1994
真分式除式的原理就是用一个数乘以除数,使被除数被足0后与得到的这个积的差小于除数.然后再用这个差补足0再重复上面的步骤.
判断从那开始循环时,大家是不是会很快地想到用类似字符比较的方法?其实,只要看是不是有和前面得出来的余数相同的余数就可以了!因为余数是会马上被当作被除数来用的,而相同的除数不变,被除数又与前面的相同,自然除得的结果会与前面的重复.太奇妙了~
if((i==49)||((i>51)&&((i+1)%50==0)))printf("/n");/*关键是这句!可以看出,第一行只有49个数字(因为包括了“.”),而第二行以后就要有50个*/
这个错可找死我了,正是练习了发现问题的能力~
main() { inta,b,yushu[1200],r,i,j; intfind; scanf("%d%d",&a,&b); while(a!=0&&b!=0) { yushu[0]=a; printf("."); i=1; find=0; while(1) { r=yushu[i-1]*10/b; yushu[i]=yushu[i-1]*10%b; printf("%d",r); if(yushu[i]==0) { find=2; break; } for(j=0;j<i;j++) { if(yushu[j]==yushu[i]) { find=1; break; } } if(find==1||find==2)break; if((i==49)||((i>51)&&((i+1)%50==0)))printf("/n"); i++; }/*insidewhile*/ if(find==1) { printf("/nThelast%ddigitsrepeatforever./n",i-j); }else{ printf("/nThisexpansionterminates./n"); } scanf("%d%d",&a,&b); }/*while*/ }/*main*/ |
相关文章推荐
- acm pku 1032 Parliament 解题报告
- acm pku 2418解题报告
- ACM pku 1007 解题报告(给像我一样的新手们的忠告)
- 九度1140解题报告
- acm pku 1321解题报告 棋盘问题
- acm pku 1423解题报告
- acm pku 2479解题报告
- ACM pku 1067 解题报告(威佐夫博奕(Wythoff Game))
- ACM pku 1005 解题报告
- ACM pku 1004 解题报告
- acm pku 1128解题报告
- ACM pku 1658 解题报告(另一种输入输出方式)
- Sum - ACM PKU 1844 解题报告
- POJ1236解题报告 强连通分支+缩点
- LeetCode — Word Break 解题报告
- LeetCode290 pattern string 解题报告
- POJ 2406 KMP 解题报告
- 【解题报告】2015ACM/ICPC亚洲区上海站
- PAT1002:1002. A+B for Polynomials 解题报告
- ZSC新生赛解题报告