forked from zhuli19901106/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-representation(AC).cpp
More file actions
76 lines (71 loc) · 1.87 KB
/
binary-representation(AC).cpp
File metadata and controls
76 lines (71 loc) · 1.87 KB
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
// What a mess...
#include <cstdlib>
using namespace std;
typedef long long int LL;
class Solution {
public:
/**
*@param n: Given a decimal number that is passed in as a string
*@return: A string
*/
string binaryRepresentation(string n) {
string s = "";
int len = n.length();
int dp;
for (dp = 0; dp < len; ++dp) {
if (n[dp] == '.') {
break;
}
}
int i = 0;
LL num = 0;
for (i = 0; i < dp; ++i) {
num = num * 10 + (n[i] - '0');
if (num > (1LL << 32) - 1) {
return "ERROR";
}
}
while (num != 0) {
s.push_back((num & 1) + '0');
num >>= 1;
}
if (s.length() == 0) {
s = "0";
}
reverse(s.begin(), s.end());
double d = atof(n.substr(dp, len - dp).data());
if (dp == len || d <= EPS) {
return s.length() <= 32 ? s : "ERROR";
}
s.push_back('.');
string f = "";
for (i = dp + 1; i < len; ++i) {
f.push_back(n[i] - '0');
}
while (f.length() > 0) {
if (f[0] >= 5) {
f[0] -= 5;
s.push_back('1');
} else {
s.push_back('0');
}
for (i = f.length() - 1; i >= 0; --i) {
f[i] *= 2;
}
for (i = f.length() - 1; i > 0; --i) {
f[i - 1] += f[i] / 10;
f[i] %= 10;
}
while (!f.empty() && f.back() == 0) {
f.pop_back();
}
if (s.length() > 64) {
// There is a bug here, but my code passed.
return "ERROR";
}
}
return s;
}
private:
const double EPS = 1e-3;
};