描述
写一个程序,输入一个形如��DN的分数,输出它的小数形式。如果小数有循环节的话,把循环节放在一对圆括号中。
例如,1331=0.33333333…写成0.(3),4133333341= 0.123123123…写成0.(123),整数x写成x.0。
通过率 71%时间限制 1000ms内存限制 256MB
考
|
数论和模拟 |
输入
输入包含两个整数N和D(1⩽�,�⩽1051⩽N,D⩽105)。
输出
输出按照上面规则计算出的小数表达式。如果结果长度大于76,每行输出76个字符。
输入样例 1复制
45 56
输出样例1复制
0.803(571428)
AC代码:
#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;
}//完结撒花
//美国的题
描述了一个涉及数论和模拟的编程题目,要求将给定分数转换为小数形式并考虑循环节。给出的C++代码展示了如何实现这一功能,包括输入解析和输出格式控制。
1151





