1.题目要求:
2.题目代码:
class Solution {
public:
string printBin(double num) {
string result;
double compare_value = 1.0;
//先给把0和.赋值给result;
result.push_back('0');
result.push_back('.');
while(result.size() <= 33){
//利用十进制转换成二进制的方法
//1.先给num * 2
num *= 2;
//2.然后取整
int temp = num;
//3.把取的整放入result以字符的形式
result.push_back(temp + '0');
//4.如果此数大于一,则减去,然后循环,直到num等会1.0
if(num > 1.0){
num -= 1.0;
}else if(num == 1.0){
break;
}
}
if(result.size() > 32){
string result1 = "ERROR";
return result1;
}else{
return result;
}
}
};