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 ...
随机推荐
- 08--C++拷贝构造函数详解
C++拷贝构造函数详解 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: [c-sharp] view plain copy int a = 100; int b ...
- 11.03 在外链接中用OR逻辑
select e.ename,d.deptno,d.dname,d.locfrom dept d left join emp e on(d.deptno = e.deptnoand (e.deptno ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(11)--使用Bundles处理简化页面代码
http://www.cnblogs.com/wuhuacong/p/4073203.html 在Web开发的时候,我们很多时候,需要引用很多CSS文件.JS文件,随着使用更多的插件或者独立样式文件, ...
- java操作Excel的poi的字体设置
package com.java.poi; import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.use ...
- 关于java中的继承
我们都知道Java中的继承是复用代码.扩展子类的一种方式,继承使得Java中重复的代码能够被提取出来供子类共用,对于Java程序的性能以及修改和扩展有很大的意义,所以这是一个非常重要的知识点. 那么对 ...
- 一键安装本地yum仓库脚本
#!/bin/bash#By:zhaocheng#Date:2019-01-18#Version v1 [ -d /media/cdrom ] || mkdir /media/cdrom[ -d /m ...
- jsp 判断当前时间是否符合设置的时间条件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- HTML表示RGB颜色的方法
NAME:red,orange,yellow,green,cyan,blue,purple RGB:rgb(r,g,b) RGBA:rgba(r,g,b,a) r.g.b.a取值范围为:0-255 H ...
- python 图片滑动窗口
METHOD #1: No smooth, just scaling. def pyramid(image, scale=1.5, minSize=(30, 30)): # yield the ori ...
- lunix下的redis数据库操作——zset有序集合
创建:(有序集合存在一个权重的概念) zadd zset 1 a 2 b 3 c 4 d 5 e 6 f 7 g # 输出: # 1) "a" # 2) "b" ...