Accept: 24 Submit: 97
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Give you a integer N and a integer D,your task is to calculate N/d and print the decimal representation. If the decimal representation has a repeating sequence of digits, indicate the sequence by enclosing it in brackets. For example, 1/3 = .33333333...is denoted as 0.(3), and 41/333 = 0.123123123...is denoted as 0.(123). Use xxx.0 to denote an integer.
For example:
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)
Input
The input consists of several test cases. Each case contains a line with two positive numbers N and D. 1 <= N,D <= 100000
Output
For each case, output the decimal expansion, as detailed above. If the expansion exceeds 76 characters in length, print it on multiple lines with 76 characters per line.
Sample Input
1 7
Sample Output
0.(142857)
Source
FOJ有奖月赛-2009年10月——稚鹰翱翔 我的程序:
#include<iostream>
#include<cmath>
using namespace std;
const int maxn=100020;
#include<cmath>
using namespace std;
const int maxn=100020;
int v[maxn];//存放余数,看是否能不能出现,要是出现就可以就停止,退出循环
int order[maxn];//记录从哪里开始循环
int ans[maxn];//存放结果
int order[maxn];//记录从哪里开始循环
int ans[maxn];//存放结果
int solve(int n,int m,int &pos)
{
memset(order,0,sizeof(order));
memset(v,0,sizeof(v));
int f=0;
int cnt=0;
v[n]=1;n*=10;
while(n)
{
if(v[n%m]==1)
{
f=1;ans[++cnt]=n/m;n%=m;
break;
}
else v[n%m]=1;
ans[++cnt]=n/m;
n%=m;
order[n]=cnt;
n*=10;
}
if(f==0)
pos=-1;
else pos=order[n];
return cnt;
}
{
memset(order,0,sizeof(order));
memset(v,0,sizeof(v));
int f=0;
int cnt=0;
v[n]=1;n*=10;
while(n)
{
if(v[n%m]==1)
{
f=1;ans[++cnt]=n/m;n%=m;
break;
}
else v[n%m]=1;
ans[++cnt]=n/m;
n%=m;
order[n]=cnt;
n*=10;
}
if(f==0)
pos=-1;
else pos=order[n];
return cnt;
}
int get(int n)
{
return log((double)n);
}
{
return log((double)n);
}
int main()
{
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int cnt;
int pos;
if(n%m==0)
{
printf("%d.0/n",n/m);
continue;
}
int zhengshu=n/m;
n%=m;
int len=solve(n,m,pos);
if(zhengshu!=0)
cnt=get(zhengshu)+1;
else
cnt=2;
if(pos==-1)
{
printf("%d.",zhengshu);
for(int i=1;i<=len;i++)
{
printf("%d",ans[i]);
cnt++;
if(cnt%76==0)
printf("/n");
}
if(cnt%76!=0)
printf("/n");
while(scanf("%d%d",&n,&m)!=EOF)
{
int cnt;
int pos;
if(n%m==0)
{
printf("%d.0/n",n/m);
continue;
}
int zhengshu=n/m;
n%=m;
int len=solve(n,m,pos);
if(zhengshu!=0)
cnt=get(zhengshu)+1;
else
cnt=2;
if(pos==-1)
{
printf("%d.",zhengshu);
for(int i=1;i<=len;i++)
{
printf("%d",ans[i]);
cnt++;
if(cnt%76==0)
printf("/n");
}
if(cnt%76!=0)
printf("/n");
}
else
{
printf("%d.",zhengshu);
for(int i=1;i<=pos;i++)
{
printf("%d",ans[i]);
cnt++;
if(cnt%76==0)
printf("/n");
}
printf("(");
cnt++;
if(cnt%76==0)
printf("/n");
for(int j=pos+1;j<=len;j++)
{
printf("%d",ans[j]);
cnt++;
if(cnt%76==0)
printf("/n");
}
printf(")");
cnt++;
if(cnt%76==0)
printf("/n");
if(cnt%76!=0)
printf("/n");
}
}
return 0;
}
printf("(");
cnt++;
if(cnt%76==0)
printf("/n");
for(int j=pos+1;j<=len;j++)
{
printf("%d",ans[j]);
cnt++;
if(cnt%76==0)
printf("/n");
}
printf(")");
cnt++;
if(cnt%76==0)
printf("/n");
if(cnt%76!=0)
printf("/n");
}
}
return 0;
}