diff --git a/cpp/2303/230302-CN.cpp b/cpp/2303/230302-CN.cpp new file mode 100644 index 0000000..5dda9d0 --- /dev/null +++ b/cpp/2303/230302-CN.cpp @@ -0,0 +1,31 @@ +#include +#include + +/** + * 面试题 05.02. Binary Number to String LCCI + * + * Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation. If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR". + */ + +class Solution { +public: + static std::string printBin(const double n) { + const auto b = *reinterpret_cast(&n); + const auto d = 1023 - ((b >> 52) & 0x7FF); + const auto e = b & 0x000FFFFFFFFFFFFF; + + const auto mask = (1ULL << (52 - (31 - d))) - 1; + if (e & mask) + return "ERROR"; + auto ret = std::string("0.").append(d - 1, '0').append(1, '1'); + for (int i = 0; i < 31 - d; ++i) + ret.push_back(((1ULL << (51 - i)) & e) ? '1' : '0'); + for (; '0' == ret.back(); ret.pop_back()); + return ret; + } +}; + +int main() { + std::cout << Solution::printBin(0.1); + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index c66e60c..c3adfe9 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230301-CN.cpp) +ADD_EXECUTABLE(2303 230302-CN.cpp)