0%

面试题 05.02. 二进制数转字符串

面试题 05.02. 二进制数转字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
string printBin(double num) {
int i = 0;
string ret = "0.";
while(num && i++ < 30)
{
num *= 2;
if(num >= 1)
{
ret.append("1");
num -= 1;
}else
ret.append("0");
}
if(num != 0)
return "ERROR";
else
return ret;
}
};