ACM pku 1140 解题报告(Expanding Fractions )

Expanding Fractions
Time Limit:1000MS  Memory Limit:10000K
Total Submit:555 Accepted:192

Description
In this problem you are to print the decimal expansion of a quotient of two integers. As you well know, the decimal expansions of many integer quotients result in decimal expansions with repeating sequences of digits. You must identify these. You will print the decimal expansion of the integer quotient given, stopping just as the expansion terminates or just as the repeating pattern is to repeat itself for the first time. If there is a repeating pattern, you will say how many of the digits are in the repeating pattern.

Input
There will be multiple input instances, each instance consists of two positive integers on a line. The first integer represents the numerator of the fraction and the second represents the denominator. In this problem, the numerator will always be less than the denominator and the denominator will be less than 1000. Input terminates when numerator and denominator are both zero.

Output
For each input instance, the output should consist of the decimal expansion of the fraction, starting with the decimal point. If the expansion terminates, you should print the complete decimal expansion. If the expansion is infinite, you should print the decimal expansion up to, but not including the digit where the repeated pattern first repeats itself.

For instance, 4/11 = .3636363636..., should be printed as .36. (Note that the shortest repeating pattern should be found. In the above example, 3636 and 363636, among others, are repeating patterns, but the shortest repeating pattern is 36.)

Since some of these expansions may be quite long, multiple line expansions should each contain exactly 50 characters on each line (except the last line, which, of course, may be shorter) - that includes the beginning decimal point.

On the line immediately following the last line of the decimal expansion there should be a line saying either ``This expansion terminates.", or ``The last n digits repeat forever.", where n is the number of digits in the repeating pattern.

Helpful hint: The number of digits before the pattern is repeated will never be more than the value of the denominator.

Sample Input

3 7
345 800
112 990
53 122
0 0 

Sample Output

 .428571
The last 6 digits repeat forever.
.43125
This expansion terminates.
.113
The last 2 digits repeat forever.
.4344262295081967213114754098360655737704918032786
885245901639
The last 60 digits repeat forever.

Source
East Central North America 1994

 


 

真分式除式的原理就是用一个数乘以除数,使被除数被足0后与得到的这个积的差小于除数.然后再用这个差补足0再重复上面的步骤.

判断从那开始循环时,大家是不是会很快地想到用类似字符比较的方法?其实,只要看是不是有和前面得出来的余数相同的余数就可以了!因为余数是会马上被当作被除数来用的,而相同的除数不变,被除数又与前面的相同,自然除得的结果会与前面的重复.太奇妙了~

 if( (i==49)  || ( (i > 51) && ((i+1)%50==0)   )     )printf("/n");   /*关键是这句!可以看出,第一行只有49个数字(因为包括了“.”),而第二行以后就要有50个*/
这个错可找死我了,正是练习了发现问题的能力~

 


 

main()
{
int a ,b ,yushu[1200] ,r ,i ,j;
int find;

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++;
 }/*inside while*/
 if(find==1)
 {
  printf("/nThe last %d digits repeat forever./n",i-j);
 }else{
  printf("/nThis expansion terminates./n");
 }
scanf("%d%d",&a,&b);

}/*while*/

}/*main*/

 

 

 

 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值