题目描述:
写一个程序,输入一个形如 N/D 的分数(N 是分子,D 是分母),输出它的小数形式。
如果小数有循环节的话,把循环节放在一对圆括号中。例如,
1/3 = .33333333 写成 0.(3)
41/333 = 0.123123123... 写成 0.(123)
用 xxx.0 成表示整数
典型的转化例子:
1/3 = 0.(3)
22/5 = 4.4
1/7 = 0.(142857)
2/2 = 1.0
3/8 = 0.375
45/56 = 0.803(571428)
输入格式
单独的一行包括被空格分开的N和D(1 <= N,D <= 100000)。
输出格式
按照上面规则计算出的小数表达式.如果结果长度大于76,每行输出76个字符.
输入输出样列
输入样例1:
45 56
输出样例1:
0.803(571428)
【耗时限制】1000ms 【内存限制】128MB
没啥可讲,看代码。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string.h>
#include <sstream>
#include <cstring>
#include <algorithm>
#include<iostream>
using namespace std;
string st;
int n,d,t,ys[100001],f=999999999,s,tt;
int main(){
cin>>n>>d;
t=n/d;
while(1){
st=(char)(t%10+'0')+st;
t/=10;
if(!t)
break;
}
st+='.';
n%=d;
while(1){
if(ys[n]){
f=ys[n];
break;
}else ys[n]=st.length();
n*=10,t=n/d;
st+=t+'0';
n%=d;
if(!n)
break;
}
if(f!=999999999)
st+=')';
for(int i=0;i<st.length();i++)
{
if(i==f&&!tt)
i--,tt=1,cout<<'(';
else
cout<<st[i];
s++;
if(s%76==0)
cout<<endl;
}
return 0;
}