题目:给你一个分数的分子和分母,求出他的循环节。
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.
#include<cstdio>
#include<cstring>
#include<stack>
#include<iostream>
using namespace std;
#define M 3000
int main()
{
int a,b;
a = b = 0;
while((cin >> a >> b)&& a+b){
printf(".");
int yushu[M] = {0},shang[M] = {0};
/// memset(shang, 0, 30);
a %= b;
a *= 10;
for(int t = 0; t < M; t++) {
shang[t] = a/b;
a = a%b;
yushu[t] = a;
a *= 10;
}
int i ,j;
for(i = 0; i < M; i++){
for(j = i + 1; j < M; j++) {
if(((shang[j] == shang[i]) && (yushu[j] == yushu[i]) )|| (i >= 100)){
goto a1;
}
}
}
a1:
if((shang[j] == 0) && ((j-i)==1)){
for(int m = 0; m < j-1; m++){
if(m % 50 == 49) printf("\n");
printf("%d",shang[m]);
}
printf("\nThis expansion terminates.\n\n");
}
else{
for(int m = 0; m < j; m++){
if(m % 50 == 49) printf("\n");
printf("%d",shang[m]);
}
printf("\n");
printf("The last %d digits repeat forever.\n\n",j-i);
}
}
return 0;
}