Sample Input
76 25
5 43
1 397
Sample Output
76/25 = 3.04(0)
1 = number of digits in repeating cycle
5/43 = 0.(116279069767441860465)
21 = number of digits in repeating cycle
1/397 = 0.(00251889168765743073047858942065491183879093198992…)
99 = number of digits in repeating cycle
C++代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<map>
using namespace std;
const int MAXN = 3000+5;
map<int,int> Pos;
void solve(int n,const int d,string& ans,int& r){
assert(n%d&&n<d);
ans=".";
Pos.clear();
while(true){
n=n*10;
int p=Pos[n];
if(p==0)
Pos[n]=ans.size();
else{
r=ans.size()-p;
if(r>50){
ans.erase(p+50);
ans+="...";
}
ans.insert(p,"(");
ans+=")";
break;
}
if(n<d){
ans+='0';
continue;
}
int div=n/d,mod=n%d;
ans+=(char)(div+'0');
n=mod;
if(n==0){
ans+="(0)";
r=1;
break;
}
}
}
int main(){
int a,b;
while(scanf("%d%d",&a,&b)==2){
string ans=".(0)";
int r=1;
if(a%b)
solve(a%b,b,ans,r);
printf("%d/%d = %d%s\n",a,b,a/b,ans.c_str());
printf(" %d = number of digits in repeating cycle\n\n",r);
}
return 0;
}