这道题相比前几道题来说,没有图,没有别的复杂算法,只是一个模拟计算过程。将结果分为两类,有限和无限循环小数。先将整数部分求出来,0或者是别的整数,然后开始求小数部分,小数部分分为循环节前和循环节后,须将他们依次求出。代码如下。
#include<bits/stdc++.h>
using namespace std;
int bas[100000];
int repeat(int x){
if (bas[x]==2){
return 1;
}
else return 0;
}
int main (){
freopen ("fracdec.in","r",stdin);
freopen ("fracdec.out","w",stdout);
int a=0,d=0;
int ans[100000],local[100000];
memset (ans,0,sizeof(ans));
memset (local,0,sizeof(local));
cin>>a>>d;
int k=a/d,cnt=0;
if (k==0){
cnt=1;
}
while (k!=0){
k=k/10;
cnt++;
}
cout<<a/d<<'.';
cnt++;
a=a%d;
bas[a]=1;
int i=1;
while (a!=0&&!repeat(a)){
local[a]=i;
a=a*10;
ans[i]=a/d;
a=a%d;
bas[a]++;
i++;
}
if (i==1){
cout<<'0'<<endl;
return 0;
}
if (a==0){
for (int j=1;j<i;j++){
cout<<ans[j];
}
cout<<endl;
return 0;
}
for (int j=1;j<local[a];j++){
cout<<ans[j];
cnt++;
}
cout<<'(';
cnt++;
for (int j=local[a];j<i;j++){
if (cnt%76==0){
cout<<endl;
}
cout<<ans[j];
cnt++;
}
cout<<')'<<endl;
return 0;
}