Cracking the Coding Interview 5.2
Given a(decimal -e.g. 3.72)number that is passed in as a string, print the binary representation. If the number can not be represented accurately in binary, print "ERROR"
整数部分:
对2取余,然后向右移动一位,重复直到整数部分变为0
小数部分:
乘以2,看结果是否大于1,大于1则2^-1位上位1,否则为0。如果大于1,将结果减1再乘以2,否则直接乘以2,继续判断,直到小数部分超过32位,返回ERROR
例如:0.75d = 0.11b,乘以2实际上相当于左移,结果大于1,则说明2^-1位上是1,然后减1,继续乘以2,结果等于1,说明2^-2上是1,且后面没有小数了。
#include<iostream> #include<string> #include<stdlib.h> using namespace std;string func(const string &str) {string strInt;string strDou;string::size_type idx = str.find('.'); if(idx == string::npos){strInt = str;}else{strInt = str.substr(0,idx);strDou = str.substr(idx);}int intPart = atoi(strInt.c_str());double douPart;if(!strDou.empty()){douPart = atof(strDou.c_str());}else{douPart = 0.0;}string strIntB,strDouB;while(intPart!=0){if((intPart&1)>0){strIntB = '1'+strIntB;}else{strIntB = '0'+strIntB;}intPart=intPart>>1;}while(!(douPart>-10e-15 && douPart<10e-15)){if(douPart*2>1){strDouB = '1'+strDouB;douPart = douPart*2-1;}else if((douPart*2-1)>-10e-15 && (douPart*2-1)<10e-15){strDouB = '1'+strDouB;break;}else{strDouB = '0'+strDouB;}if(strDouB.size()>32){return "ERROR";}}if(strDouB.empty()){return strIntB;}else{return (strIntB+'.'+strDouB);} }int main() {string str("3.75");cout<<func(str)<<endl;return 0; }
转载于:https://www.cnblogs.com/johnsblog/p/3923016.html
Cracking the Coding Interview 5.2相关推荐
- [Free] Cracking the Coding Interview 6th Download
[Free] Cracking the Coding Interview 6th Download 推荐给有梯子的童鞋们! https://www.pdfdrive.com/cracking-the- ...
- Cracking the coding interview
转自:http://hawstein.com/posts/ctci-solutions-contents.html Cracking the coding interview--问题与解答 March ...
- 经典算法题目:Cracking the coding interview 问题与解答
Cracking the coding interview--问题与解答 March 14, 2013 作者:Hawstein 出处: http://hawstein.com/posts/ctci-s ...
- 渣基础:比照Hawstein学Cracking the coding interview(1)
<C++ Primer 第五版>书实在是太长,太厚了.总是看了十几页就看累了,坚持不了多久,想了想还是别勉强自己,决定把它当工具书查看,或者积累足够的C++经验后再翻阅一遍. 目前的打算是 ...
- [Cracking the Coding Interview] 4.4 Check Balanced
Implement a function to check if a binary tree is balanced. For the purpose of this question, a bala ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由'0'或者'1'构成的二维数组,找出一个四条边全部由'1'构成的正方形(矩形中间可以有'0'),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目5
2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...
- 《Cracking the Coding Interview》——第18章:难题——题目3
2014-04-29 01:02 题目:从m个整数里随机选出n个整数,要求等概率. 解法:和洗牌的算法类似,每次随机抽出一个数,抽n次即可.时间复杂度O(m * n),空间复杂度O(m). 代码: 1 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目3
2014-04-27 19:26 题目:哲学家吃饭问题,死锁问题经典模型(专门用来黑哲学家的?). 解法:死锁四条件:1. 资源互斥.2. 请求保持.3. 非抢占.4. 循环等待.所以,某砖家拿起一只 ...
- Cracking The Coding Interview 9.1
//原文: // // You are given two sorted arrays, A and B, and A has a large enough buffer at the end to ...
最新文章
- mysql 分析执行计划的效率_MySQL执行计划分析
- ACM-ICPC 2018徐州网络赛-H题 Ryuji doesn't want to study
- IntelliJ IDEA常用快捷键——基于Eclipse
- Django3Vue3前后端分离项目
- \045在字符串中输出为%
- spring生命周期七个过程_想要学会Spring源码,你必知必会的BeanDefinition原理!
- Map集合常见面试题
- 卡西欧手表城市编码,调时区用
- linux开发屏幕保护代码,使用xscreensaver编写屏幕保护程序的提示和技巧?
- SD nand flash 简单的读写速度测试
- 【测试能力提升】Jira 和禅道数据库分析,方便你写周报、写总结、出报告
- ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices (翻译)
- Win11系统电脑怎么C盘扩容教学
- 【专访】黄文强:电子商务创新航企营销模式
- 例子, 防火墙配置domian比较策略
- 《嵌入式 - Lwip开发指南》第5章 LWIP测速
- 乔布斯经典语录:洗尽铅华的感悟
- 【Planning】R3 PTF and firm mapped with APO
- 熊瞎子错把虎斑猫看成老虎
- outlook 不能打开错误解决方法
热门文章
- 全志A33-USB虚拟网卡的配置与使用
- 基础003_V7-Memory Resources
- 一发工资就全部取出,会对银行流水有影响吗?
- verilog中assign语句
- 为什么不敢和别人竞争_内心很脆弱,不敢和人竞争。该怎么办?
- 动物识别专家系统python_Python有哪些作用?
- matlab二项式拟合,数据回归分析和拟合的Matlab实现
- macos可以升级到指定版本吗_iOS14如期而至!重大更新的全新版本,值得升级吗?答案在这...
- CAT arguments dimensions are not consistent.CAT参数的维度不一致。
- 【caffe-Windows】以mnist为例的hdf5单标签处理——matlab实现