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 添加列,修改列,删除列
创建后表的修改 alter table 语句用于创建后对表的修改, 基础用法如下: 添加列 基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置]; 示例: 在表 ...
- 初识mySQL(关系型数据库)
一.数据库修改密码 ①先执行use mysql; ②再执行update mysql.user set password=PASSWORD(要修改的密码) where user='root' ; ...
- Sed简单入门实例
1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后 ...
- Win 環境構建調試用TreeWalker
語法 var ppTreeWalker = document.createTreeWalker(pRootNode, ulWhatToShow, pFilter, fEntityReferenceEx ...
- SpringSecurity相关配置【SpringSecurityConfig】
SpringSecurity的配置相对来说有些复杂,如果是完整的bean配置,则需要配置大量的bean,所以xml配置时使用了命名空间来简化配置,同样,spring为我们提供了一个抽象类WebSecu ...
- codefoces round193a
link: http://codeforces.com/contest/332/problem/A /* ID: zypz4571 LANG: C++ TASK: 193a.cpp */ #inclu ...
- URAL 1080 Map Coloring(染色)
Map Coloring Time limit: 1.0 secondMemory limit: 64 MB We consider a geographical map with N countri ...
- Xcode7.0.1(ios9)的部分适配问题
今天更新了Xcode 7 正式版,App编译出现很多警告,在App运行的时候出现如下的提示......... the resource could not be loaded because the ...
- 兼容FF 加入收藏夹和设为首页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【转】iOS10项目打包上传被拒关于隐私权限问题
原文网址:http://blog.csdn.net/yidu_blog/article/details/53064987 今天项目打包提交.收到了苹果的邮件.主要内容: This app attemp ...