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 ...
随机推荐
- Linux内核分析 计算机是如何工作的——by王玥
1.冯诺依曼体系结构:也就是指存储程序计算机 硬件(存储程序计算机工作模式): 软件(程序员角度): 2.API:程序员与计算机的接口界面 ABI:程序与CPU的接口界面 3.X86的实现: 4.X8 ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- Spring+Junit测试用例的使用
1.[导包]使用Spring测试套件,需要两个jar包:junit-X.X.jar和spring-test-X.X.X.RELEASE.jar,在maven项目下可添加如下依赖: <depend ...
- RDM 使用与破解
RDM 的下载地址 https://cdn.devolutions.net/download/Setup.RemoteDesktopManager.13.6.2.0.msi#_ga=2.2471513 ...
- 苹果手机input框上方有一条阴影线以及input框的placeholder颜色的设置
今天做手机端的时候,用到input框来输入手机号码,但是在安卓手机上input的效果是正常的,在苹果手机上,input的上边框会变粗,有阴影 因为苹果手机的默认给input加上了阴影 给input加入 ...
- Spring注解开发简要步骤
1.除spring基本包外还需要下载AOP包 spring-aop-4.2.4.RELEASE.jar 2.导入约束(最后两行) <beans xmlns="http://www.sp ...
- Json序列化循环引用的问题
今天在发布接口的时候出突然出现了一个问题,报错代码为: 1 An exception has occurred while using the formatter 'JsonMediaTypeForm ...
- idea中 读取mybatis的配置文件时候 如果放在文件下面 需要加上路径
- 01 Maven构建的项目中,把.xml等配置文件添加到编译目录
Maven构建的项目,默认只会把src/main/resources目录下的xml配置文件添加到编译目录. 如果需要把src/main/java目录下的xml配置文件也添加到编译目录,需要在pom.x ...
- ASP.NET MVC学习之Log4Net配置(日志记录)
Log4Net配置笔记---- 首先,添加对log4net.dll的引用. 在Web.config文件下的Configuration节点下添加Log4Net的配置信息: <!--Log4Net配 ...