LintCode "Binary Representation"
Not hard to think of a solution. But the key is all details.
class Solution {
public:
    /**
     *@param n: Given a decimal number that is passed in as a string
     *@return: A string
     */
    string binaryRepresentation(string n) {
        size_t pos = n.find(".");
        unsigned long vi = atoi(n.substr(, pos).c_str());
        double vf = atof(n.substr(pos).c_str());
        //  Int part
        string si;
        while(vi)
        {
            si = ((vi & ) ? '' : '') + si;
            vi >>= ;
        }
        if(si.empty()) si = "";
        if(vf == .) return si;
        //  Fractional part
        string sf;
        while (vf > 0.0)
        {
            if (sf.length() > ) return "ERROR";
            if (vf >= 0.5) {
                sf += '';
                vf -= 0.5;
            }
            else
            {
                sf += '';
            }
            vf *= ;
         }
        return si + "." + sf;
    }
};
LintCode "Binary Representation"的更多相关文章
- [CareerCup] 5.2 Binary Representation of Real Number 实数的二进制表示
		
5.2 Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary ...
 - [CareerCup] 5.3 Next Binary Representation 下一个二进制表达
		
5.3 Given a positive integer, print the next smallest and the next largest number that have the same ...
 - [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
		
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
 - [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
		
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
 - 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
		
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
 - LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告
		
题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
 - [LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation
		
Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...
 - Binary Representation
		
Given a (decimal - e.g. 3.72) number that is passed in as a string, return the binary representation ...
 - 1.求整数最大的连续0的个数  BinaryGap Find longest sequence of zeros in binary representation of an integer.
		
求整数最大的连续0的个数 A binary gap within a positive integer N is any maximal sequence of consecutive zeros t ...
 
随机推荐
- MySQL的Explain解释器的部分理解
			
Explain 部分说明进行解释 (1) Extra列的Using Where 表示在进行过滤后在进行Where语句的过滤 (2) type为ref,直接按索引顺序返回,没有 Using fileso ...
 - hadoop常用基础命令
			
1.日志查询: 用户可使用以下命令在指定路径下查看历史日志汇总$ bin/hadoop job -history output-dir 这条命令会显示作业的细节信息,失败和终止的任务细节. 关于作业的 ...
 - Spring mvc源码url路由-我们到底能走多远系列(38)
			
我们到底能走多远系列38 扯淡: 马航的事,挺震惊的.还是多多珍惜身边的人吧. 主题: Spring mvc 作为表现层的框架,整个流程是比较好理解的,毕竟我们做web开发的,最早也经常接触的就是一个 ...
 - javascript 字符串方法名调用
			
项目中有时候需要通过字符串传递方法名称,供页面调用 var ParameterDefaultCallMethod = Request("ParameterDefaultCallMethod& ...
 - vim 把满足条件的数字进行加上一些数字
			
1,1,1,n4s-1 1,3,4,n3s= 1,4,6,e4h= 1,5,8,e4h-1 1,6,2,e3ntx-2 1,7,5,n1s+2 1,8,7,n3s= 比如以上的数据格式以“,”为列 ...
 - JavaScript 编写多线程代码引用Concurrent.Thread.js(转)
			
这是一个很简单的功能实现: <script type="text/javascript" src="Concurrent.Thread.js">&l ...
 - TensorFlow简单介绍和在centos上的安装
			
##tensorflow简单介绍: TensorFlow™ is an open source software library for numerical computation using dat ...
 - 本文使用springMVC和ajax,实现将JSON对象返回到页面
			
一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...
 - IOS开发之SWIFT
			
Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了 ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在其中 ...
 - 英语语法最终珍藏版笔记-17名词性-主语-宾语-同位语-表语-that从句
			
名词性从句 在主从复合句中,从句可以充当主句的主语.表语.宾语或同位语.由于在多数情况下,主语.表语.宾语或同位语这四种句子成分由名词性词类充当,所以,我们把这些作用相当于名词的从句统称为名词性从句, ...