Binary Representation
Given a (decimal - e.g. 3.72) number that is passed in as a string, return the binary representation that is passed in as a string. If the fractional part of the number can not be represented accurately in binary with at most 32 characters, return ERROR.
For n = "3.72", return "ERROR".
For n = "3.5", return "11.1".
分析:
分成两个部分,整数部分用%和/,小数部分 *2 如果 <1 就为0,基数=基数;大于1,就为1,基数=基数-1
比如:
0.6*2=1.2>0 那么就为1 基数=1.2-1=0.2 0.2*2=0.4<0 那么就为0,基数=0.4 0.4*2=0.8<0,那么就为0,基数=0.8 0.8*2=1.6>0 那么就为1,基数为1.6-1=0.6
:
:
:
:
所以5.6可以表示为:101.1001
public class Solution {
/**
*@param n: Given a decimal number that is passed in as a string
*@return: A string
*/
public String binaryRepresentation(String n) {
int intPart = Integer.parseInt(n.substring(, n.indexOf('.')));
double decPart = Double.parseDouble(n.substring(n.indexOf('.')));
String intstr = "";
String decstr = "";
if (intPart == ) intstr += '';
while (intPart > ) {
int c = intPart % ;
intstr = c + intstr;
intPart = intPart / ;
}
int count = ;
while (decPart > 0.0 && count <= ) {
double r = decPart * ;
if (r >= 1.0) {
decstr += '';
decPart = r - 1.0;
}else {
decstr += '';
decPart = r;
}
count++;
}
if (count > ) return "ERROR";
return decstr.length() > ? intstr + "." + decstr : intstr;
}
}
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 ...
- 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 ...
- 【Leetcode_easy】762. Prime Number of Set Bits in Binary Representation
problem 762. Prime Number of Set Bits in Binary Representation solution1: class Solution { public: i ...
随机推荐
- 团队作业五之旅游行业手机APP分析
深入分析旅游业手机APP——分析员王奕 在接到组长分配的任务的时候,我的内心是激动的.因为自己不擅长编程,所以还是比较喜欢这种“外围”的文字工作.但是,面对数量庞大的旅游业APP,一时间自己真的不知 ...
- Mybatis 从入门到精通一:mybatis的入门
1.Mybatis简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation(阿帕奇软件基金会) 迁移到了google ...
- Aqua Data Studio 数据库开发工具
Aqua Data Studio是一款完整IDE的数据库开发工具,它提供3种主要功能:数据查询与管理工具.比对数据工具与源控制和文件系统的整合工具.帮助你创建,编辑和执行 SQL 的管理工具脚本编写, ...
- OneZero团队Beta发布剧透
统计界面暂不透露,尽请期待.
- 深入理解Java反射+动态代理
答: 反射机制的定义: 是在运行状态中,对于任意的一个类,都能够知道这个类的所有属性和方法,对任意一个对象都能够通过反射机制调用一个类的任意方法,这种动态获取类信息及动态调用类对象方法的功能称为j ...
- selectTree & bug
selectTree & bug 相对路径 & 绝对路径 http://192.168.58.189:8080/hui/#/components/selectTree https:// ...
- Java之扫描目录,修改文件内容
扫描目录下文件,修改文件中指定内容 package org.utils.tools.fileoper; import java.io.*; import java.util.ArrayList; im ...
- linux、windows搭建nginx出现问题集锦
1.启动提示端口被占用(linux) 启动ninx出现nginx: [emerg] bind() to0.0.0.0:80 failed (98: Address already in use) ne ...
- 【刷题】BZOJ 4945 [Noi2017]游戏
Description http://www.lydsy.com/JudgeOnline/upload/Noi2017D2.pdf Solution 字符串里的'x'看起来很烦,于是考虑枚举这些'x' ...
- zk会话,快照,序列化,本地存储
FolloewerRequestProcessor类 追随者 输入会有不同的形式,客户端请求,提议,提交事务 通过箭头来标示追随者处理的不同路径 本地存储 事务日志和快照 SyncRequestPro ...