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的更多相关文章

  1. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  2. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  3. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  4. 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 ...

  5. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  6. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  7. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  8. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  9. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  10. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

随机推荐

  1. MSP430之software development flow

    MSP430 software development flow. 1) The shaded portion highlights the most common development path; ...

  2. 使用DOM解析XML文档

    简单介绍一下使用DOM解析XML文档,解析XML文件案例: <?xml version="1.0" encoding="UTF-8"?> -< ...

  3. JDBC Druid式link

    准备工作:导入包------druid-1.0.9.jar    src文件夹下放下druid.properties文件 且其中的url和数据库名要配置完备 import JdbcUtils.JDBC ...

  4. AdminLTE框架基础布局使用

    boxbox-solid:去掉顶部边框线box-headerwith-border:添加头底部边框线 按钮:—— btn btn-default 默认<div class="btn-g ...

  5. esp32(M5STACK)在线体验(Ubuntu)

    我们往m5stack烧录的固件是可以在线编程的 具体使用方法可以参考   https://github.com/m5stack/M5Cloud/blob/master/README_CN.md     ...

  6. 08.Web服务器-3.Web静态服务器

    1.显示固定的页面 from socket import * from multiprocessing import * import os def handleClient(clientSocket ...

  7. 用 Vue 做一个简单的购物app

    前言 最近在学习Vue的使用.看了官方文档之后,感觉挺有意思的.于是着手做了一个简单的购物app.h5 与原生 app 交互的原理这是我第一次在这个网站上写分享,如有不当之处,请多多指教. 一整个项目 ...

  8. ThinkPhp5.0 引入全局自定义函数global

    可以直接调用:相当于global.class.php 全局文件 ==================================================================== ...

  9. 使用阿里云对象存储OSS上传图片工具类

    package com.verse.hades.utils; import com.aliyun.oss.OSSClient; import com.aliyun.oss.common.auth.Cr ...

  10. 【ACM】hdu_1170_Balloon Comes!_201307261946

    Balloon Comes!Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...