您的位置:首页 > 其它

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

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*/




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