-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfracdec2.cpp
79 lines (70 loc) · 1.54 KB
/
fracdec2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
ID:kevin_s1
LANG:C++
TASK:fracdec
*/
#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
using namespace std;
int gcd(int x,int y)
{
if (x==0) return y;
return gcd(y%x,x);
}
int main()
{
ifstream fin("fracdec.in");
ofstream fout("fracdec.out");
int n,d,k[100001],dat[100001],len=0;
fin>>n>>d;
fout<<n/d<<".";
if (n>=d) len=int(log(n/d)/log(10)+2.00001);
else len=2;
n%=d;
memset(k,0,sizeof(k));
int tmp=d/gcd(n,d);
while (tmp%2==0) tmp/=2;
while (tmp%5==0) tmp/=5;
if (tmp==1)
{
for (;;)
{
n*=10;
fout<<n/d;
n%=d;
if (n==0) break;
}
fout<<endl;
return 0;
}
for (int i=1;;i++)
{
if (k[n]!=0)
{
for (int j=1;j<=k[n]-1;j++)
{
fout<<dat[j];
len++;
if (len%76==0) fout<<endl;
}
fout<<"(";
len++;
if (len%76==0) fout<<endl;
for (int j=k[n];j<=i-1;j++)
{
fout<<dat[j];
len++;
if (len%76==0) fout<<endl;
}
fout<<")"<<endl;
return 0;
}
k[n]=i;
n*=10;
dat[i]=n/d;
n%=d;
}
return 0;
}