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 ...
随机推荐
- 用jquery将复选框改成单选框
前提是要包含jquery文件. 相关代码: jQuery(function($) { init_price_checkbox("by_price"); init_price_che ...
- spark streaming 实现接收网络传输数据进行WordCount功能
package iie.udps.example.operator.spark; import scala.Tuple2; import org.apache.spark.SparkConf; imp ...
- Windows RPC Demo实现
Windows RPC Demo实现 本文参考并整理以下相关文章 1. <远程过程调用> -百度百科 2. <RPC 编程> -http://www.ibm.com/devel ...
- Java 有理数类 分数类 Rational类的设计与实现
要实现Rational类的加减乘除,要实现其可比较性,要覆盖toString()方法,要实现不同数据类型的转换等. package chapter14; public class Rational e ...
- JAVA 遍历文件夹下的所有文件
JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...
- web app 的技术参考 -- 来自 【百度移动建站指南】
优化页面性能 考虑到移动设备和移动互联网的特点,在进行移动网站的页面开发设计时,一个总的原则是考虑用户访问的效率,降低页面加载时间. 下面的内容来自百度,然后我自己做了笔记.另外可参考这个系列的文章 ...
- andorid中Html.fromHtml方法
在android中,有一个容易遗忘的Html.fromhtml方法,意思是可以将比如文本框中的字符串进行HTML格式化,支持的还是很多的, 但要注意的是要在string.xml中用<!--cda ...
- android中获取string字符串的方法
比如在arrays.xml里: <!--leo added for KYLIN-496--> <string-array name="reboot_item"&g ...
- java如何获取当前机器ip和容器port
获取当前机器ip: private static String getIpAddress() throws UnknownHostException { InetAddress address = I ...
- 拖放API
拖放功能是电脑用户认为理所应当能够“顺畅运行”的功能,我们有数种方法在浏览器中启用此功能.Windows Internet Explorer 9 和早期版本的 Windows Internet Exp ...