小數的二進位制表示

2021-09-08 04:15:50 字數 703 閱讀 4147

兩種方法,注意邏輯細節!

//

題目描述

////

有乙個介於0和1之間的實數,型別為double,返回它的二進位制表示。如果該數字無法精確地用32位以內的二進位制表示,返回「error」。

//給定乙個double num,表示0到1的實數,請返回乙個string,代表該數的二進位制表示或者「error」。

//測試樣例:

//返回:0.101

//兩種方法,正向和逆向

#include

using

namespace

std;

#include

class

bindecimal

else

if (str.size()>=32

)

//num -= base; 邏輯錯誤

base=base/2

; }

return

str;

}};class

bindecimal

else

if (num<=0.0

)

}if (num>0.0

)

return

str;

}};int

main()

LintCode 二進位制表示

給定乙個數將其轉換為二進位制 均用字串表示 如果這個數的小數部分不能在 32 個字元之內來精確地表示,則返回 error 樣例 n 3.72 返回 error n 3.5 返回 11.1 題目很簡單,直接上 吧 public class binaryrepresention return res.r...

LintCode 二進位制表示

給定乙個數將其轉換為二進位制 均用字串表示 如果這個數的小數部分不能在 32 個字元之內來精確地表示,則返回 error 您在真實的面試中是否遇到過這個題?yes 樣例n 3.72 返回 error n 3.5 返回 11.1 好久沒做題 有點懵逼 根據資料試出來的 臥槽 public class ...

二進位制表示 LintCode

給定乙個數將其轉換為二進位制 均用字串表示 如果這個數的小數部分不能在 32 個字元之內來精確地表示,則返回 error 樣例 n 3.72 返回 error n 3.5 返回 11.1 ifndef c180 h define c180 h include include using namesp...