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. 新手上路 git你好

    天哪,虽然我是一个学计算机的,但是我发现我的计算机学的真是……好吧不说了,言归正传.​ 这几天一直在着手于git,可能只是学了一个皮毛,结果也是不大尽人意,跟着别人学了学,鼓捣了鼓捣,还是有点小小的收 ...

  2. 第二个spring冲刺第9天

    其中一个队员在检查程序的BUG途中发现了几个重要的BUG比如答案乱码.程序闪退,弹出黑幕.于是我们决定先把这些问题解决再继续开发其他功能

  3. 回忆--RYU流量监控

    RYU流量监控 前言 Ryu book上的一个流量监控的应用,相对比较好看懂 实验代码 github源码 from ryu.app import simple_switch_13 from ryu.c ...

  4. 更新pip10后 ImportError: cannot import name ‘main'

    百度了几个回答都没有解决问题,有些回答明显是直接复制过来的一点价值都没有,然后google一下立马解决.很多时候不能怪搜索引擎,问题出在一些国内网友对知识的不负责任 解决:找到报错文件,也就是那个pi ...

  5. 点分治&动态点分治小结

    (写篇博客证明自己还活着×2) 转载请注明原文地址:http://www.cnblogs.com/LadyLex/p/8006488.html 有的时候,我们会发现这样一类题:它长得很像一个$O(n) ...

  6. fgt2eth Script

    fgt2eth Script explanation_on_how_to_packet_capture_for_only_certain_TCP_flags_v2.txt Packet capture ...

  7. oracle 获取当前用户下的所有表名与字段信息

     select *from user_col_commentswhere substr(table_name,1,3)<>'BIN' 

  8. 【bzoj3132】 Sdoi2013—森林

    http://www.lydsy.com/JudgeOnline/problem.php?id=3123 (题目链接) 题意 给出$n$个点的森林,每个点有一个非负点权,$m$个操作.连接两个点,查询 ...

  9. 《剑指offer》— JavaScript(30)连续子数组的最大和

    连续子数组的最大和 题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好 ...

  10. haoi2006_受欢迎的牛_Solution

    Brief Solution: 强连通tarjan+压缩点+判断是否除了一个点,其它点都有出度 Detailed Solution: 把牛看成点若一个点b能到达点a,则b认为a受欢迎若所有的点都能到达 ...