给定一个double num,范围是0到1的实数,若其能用32位以内的二进制表示出来则返回值string为其二进制值,若不能则返回值为“Error”。
测试样例:
0.625
返回:0.101
class BinDecimal {
public:
string printBin(double num) {
// write code here
string res = "0.";
while(num){
num *= 2;
if(num >= 1){
res += '1';
num -= 1;
}
else res += '0';
if(res.size() > 32) return "Error";
}
return res;
}
};