-
Notifications
You must be signed in to change notification settings - Fork 0
/
42.txt
62 lines (61 loc) · 1.25 KB
/
42.txt
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
#include<iostream>
#include<sstream>
#include<vector>
#include<string>
using namespace std;
string decimal(int,int);
string Int_to_String(int n)
{
ostringstream stream;
stream<<n;
return stream.str();
}
int main() {
int N,D;
cin>>N>>D;
int integer = N/D;
cout<<integer<<"."<<decimal(N%D,D)<<endl;
return 0;
}
string decimal(int n,int d) {
if(n == 0)
return "0";
vector<int> loop;
vector<int> quotient;
loop.push_back(n);
int remainder;
bool flag = false;
int pos; // 记录循环体开始位置,加(
do {
remainder = n*10%d;
int i = 0;
for(;i<loop.size();i++) {
if(remainder == loop[i]) {
pos = i;
break;
}
}
if(i != loop.size()) {
flag = true;
quotient.push_back(n*10/d);
break;
}
loop.push_back(remainder);
quotient.push_back(n*10/d);
n = remainder;
} while(n != 0);
string s = "";
for(int i=0;i<pos;i++) {
s += Int_to_String(quotient[i]);
}
if(flag) {
s += "(";
}
for(int i=pos;i<quotient.size();i++) {
s += Int_to_String(quotient[i]);
}
if(flag) {
s += ")";
}
return s;
}