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 ...
随机推荐
- hdu 5093 Battle ships
二分图匹配 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> ...
- matlab演奏最炫民族风的代码注释
用Matlab来放音乐,和用单片机加蜂鸣器放音乐的原理都差不多,就是把连续的声音信号事先转换成用数字信号,然后用扬声器按照一定的节奏放出来.换句话说,演唱者是把声音经过麦克风转换成电信号,录音设备对这 ...
- 转 用 AXIOM 促进 XML 处理
转自:http://www.ibm.com/developerworks/cn/xml/x-axiom/ AXIOM 还不是另一种对象模型.它有着明确的设计目标:大幅提升 Apache 下一代 SOA ...
- hive学习笔记_hive的介绍与安装
一.什么是Hive Hive是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据 ...
- 【转】IOS图像拉伸解决方案
原文网址:http://www.cnblogs.com/ios8/p/ios-pic-lashen.html UIButton实现背景拉伸,即图片两端不拉伸中间拉伸的办法有如下两种: 第一种方法很简单 ...
- css animation让图标不断旋转
@keyframes rotating{from{transform:rotate(0)}to{transform:rotate(360deg)}} animation:rotating 1.2s l ...
- CUDA学习笔记(二)【转】
来源:http://luofl1992.is-programmer.com/posts/38847.html 编程语言的特点是要实践,实践多了才有经验.很多东西书本上讲得不慎清楚,不妨自己用代码实现一 ...
- 我今天也学习了做jquery插件
先贴代码 (function ( $ ) { var id=33; $.fn.validate=function(options){ // This is the easiest way to hav ...
- elasticsearch介绍集群,模拟横向扩展节点、节点宕机、改变分片
出处:[http://www.cnblogs.com/dennisit/p/4133131.html] ,防楼主删博,故保留一份! elasticsearch用于构建高可用和可扩展的系统.扩展 ...
- easyUI之message
message组件,依赖于window组件.progressbar组件两个面板. 它有几个不同的显示风格,与vb中的msgbox相对应,有alert.progrss.confirm.prompt等形式 ...