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.

Example

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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. DataGridView不显示未绑定的列-AutoGenerateColumns

    DataGridView绑定数据源时,会自动显示未绑定的列,怎么让其不显示未绑定的列呢? 设置AutoGenerateColumns属性即可 this.dataGridView1.AutoGenera ...

  2. js弹出层学习

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. Docker 下 mysql 简单的 主从复制实现

    1. 拉取镜像 docker pull mysql: 2. 运行这个镜像 docker run -d --name maser mysql: 3. 安装一些必要的软件 docker exec -it ...

  4. Windows 下 Docker 的简单学习使用过程之一 dockertoolbox

    1. Windows 下面运行 Docker 的两个主要工具1): Docker for Windows2): DockerToolbox区别:Docker For Windows 可以理解为是新一代 ...

  5. ES6 数值的扩展

    在ES5中,我们存在几个全局函数 isNaN函数,isFinite函数,parseInt函数,parseFloat函数等,对于这些全局函数的使用很简单,就拿isNaN函数来举例. ES5中的写法是: ...

  6. Android GridView数据绑定

    java代码构造个泛型数组用于存放item,作为title        List<Map<String, Object>> items = new ArrayList< ...

  7. (转)JDK1.8新特性Lambda表达式

    https://www.cnblogs.com/franson-2016/p/5593080.html Predicate predicate接收一个变量,并返回一个boolean值,predicat ...

  8. BZOJ4589 Hard Nim(博弈+FWT)

    即使n个数的异或为0.如果只有两堆,将质数筛出来设为1,做一个异或卷积即可.显然这个东西满足结合律,多堆时直接快速幂.可以在点值表示下进行. #include<iostream> #inc ...

  9. BZOJ4036 HAOI2015按位或(概率期望+容斥原理)

    考虑min-max容斥,改为求位集合内第一次有位变成1的期望时间.求出一次操作选择了S中的任意1的概率P[S],期望时间即为1/P[S]. 考虑怎么求P[S].P[S]=∑p[s] (s&S& ...

  10. #35 string(缩点+动态规划)

    容易发现有了交换相邻字符的操作后,只要字符串所含有的字符种类和数量相同其就是等价的.这样的状态只有n^3级别,将其抽象成点子串变换抽象成边后就是求最长路径了,缩点dp解决. 码量巨大,不是很明白要怎样 ...