英文题意很难读,但还是自己读吧
因为在循环小数中,只要出现相同的模数,就代表着出现循环节了
而题目中保证除数小于等于3000
所以最多3000位就会出现循环
直接用数组保存一下出现位置就可以了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read(){
int x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int dp[3005];
int ans_int,ans_fl[6005];
int main(){
int a,b;
while(scanf("%d %d",&a,&b)!=EOF){
printf("%d/%d = ",a,b);
memset(dp,-1,sizeof(dp));
ans_int=a/b;a%=b;
printf("%d.",ans_int);
int s=0,t=0;
dp[a]=0;
for(int i=1;;i++){
a*=10;
ans_fl[i]=a/b;
a%=b;
if(dp[a]!=-1){
s=dp[a]+1,t=i;
break;
}
dp[a]=i;
// if(i>=6005) while(1);
}
for(int i=1;i<s;i++) printf("%d",ans_fl[i]);
printf("(");
if(t>50){
for(int i=s;i<=50;i++) printf("%d",ans_fl[i]);
puts("...)");
}
else{
for(int i=s;i<=t;i++) printf("%d",ans_fl[i]);
puts(")");
}
printf(" %d = number of digits in repeating cycle\n",t-s+1);
puts("");
}
return 0;
}