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(,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!=)
{
if((intPart&)>)
{
strIntB = ''+strIntB;
}
else
{
strIntB = ''+strIntB;
}
intPart=intPart>>;
}
while(!(douPart>-10e- && douPart<10e-))
{
if(douPart*>)
{
strDouB = ''+strDouB;
douPart = douPart*-;
} else if((douPart*-)>-10e- && (douPart*-)<10e-)
{
strDouB = ''+strDouB;
break;
} else
{
strDouB = ''+strDouB;
}
if(strDouB.size()>)
{
return "ERROR";
}
} if(strDouB.empty())
{
return strIntB;
}
else
{
return (strIntB+'.'+strDouB);
}
} int main()
{
string str("3.75");
cout<<func(str)<<endl;
return ;
}
Cracking the Coding Interview 5.2的更多相关文章
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第5章:位操作——题目7
2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)
#include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...
随机推荐
- eclipse快捷键:
打开快捷键提示: ctrl + shift + L; 自动补全代码: Alt + /; 快速修复: ctrl + 1; 导包: ctrl + shift + o; 格式化代码: ctrl + shif ...
- 理解Python中编码的应用
完全理解字符编码 与 Python 的渊源前,我们有必要把一些基础概念弄清楚,虽然有些概念我们每天都在接触甚至在使用它,但并不一定真正理解它.比如:字节.字符.字符集.字符码.字符编码. 字节 字节( ...
- Unicode转换为UTF-8过程Demo
碎碎念:这几天在学习Python对Unicode的支持 上学的时候,计算机基础课上总能听到老师讲什么字节,字符,Unicode,UTF-8吧啦吧啦一堆,反正我是只记住了名字,至于具体这些名字所表达的含 ...
- day35-1 类的三大特性---继承,以及类的派生
目录 类的继承 继承的特性 类的派生 类的组合 类的继承 继承是为了拿到父类的所有东西 继承的特性 减少代码的冗余 Python中父类和子类的对应关系是多对多 使用__bases__方法获取对象继承的 ...
- PAT_A1105#Spiral Matrix
Source: PAT A1105 Spiral Matrix (25 分) Description: This time your job is to fill a sequence of N po ...
- 【剑指Offer】34、第一个只出现一次的字符
题目描述: 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写). 解题思路: ...
- 第三次组队赛 (DFS&BFS)
网站:CSUST 8月1日 先总结下,不得不说死的很惨,又是第三就不说了,一共7道题,AC了5道,但是有一个组三个人是做的个人赛,有两人AK了.......Orz,然后深搜还是大问题,宽搜倒是不急了. ...
- java陷阱之spring事物管理导致锁无效
模拟锁情况无效 1.创建一个表 SET NAMES utf8mb4; ; DROP TABLE IF EXISTS `demo`; CREATE TABLE `demo` ( `id` ) NOT N ...
- Redis命令操作简介及五种value数据类型
转自:https://blog.csdn.net/ty4315/article/details/52050721 Redis是使用键值存储数据,key必须是字符串value支持五种数据类型,最新版本又 ...
- chrome js 获取css
var myDiv = document.getElementById("chooseRect"); var computedStyle = document.defaultVie ...