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 ...
随机推荐
- [福大软工] Z班 团队作业——UML设计 作业成绩
团队作业--UML设计 作业链接 http://www.cnblogs.com/easteast/p/7745703.html 作业要求 1)团队分工(5分) 描述团队的每个成员分别完成了UML图的哪 ...
- 软件工程作业 - Week 1
构建之法读后疑问: 初步的完成构建程序设计思路之后实现过程中发现了问题或者可以优化的地方是立马就改进还是完成之后按照步骤统一进行优化. 覆盖性测试,针对一些永远用不到只是用来预防极为极端的情况下,例如 ...
- 用友时空KSOA功能挖掘之zl_func函数
问题日常开发中,需要对界面进行控制,不符合条件时禁用某些功能菜单.例如[采购订单填制]界面,要实现供应商资质证书效期提醒功能,即近效期提醒,超效期禁止采购,如何实现呢? 分析使用KSOA新增加的zl_ ...
- python netifaces usage
1. install python dev sudo apt-get install python-dev 2.download src code and install https://pypi.p ...
- Tyvj1038 忠诚 (线段树)
[Tyvj1038]忠诚 线段树 题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意.但是 ...
- Y组合子
Y组合子 Y组合子的用处 作者:王霄池链接:https://www.zhihu.com/question/21099081/answer/18830200来源:知乎著作权归作者所有.商业转载请联系作者 ...
- MT【193】三面角的正余弦定理
(原题为浙江名校新高考研究联盟2018届第三次联考选择压轴题) 在平面$\alpha$内,已知$AB\perp BC$,过直线$AB,BC$分别作平面$\beta,\gamma$,使得锐二面角$\al ...
- 【题解】 [HNOI2015]菜肴制作 (拓扑排序)
题目描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予1到N的顺序编号,预估质量最高的菜肴编号为1. 由于菜肴 ...
- Elastic-Job-Lite 源码分析 —— 运维平台
本文基于 Elastic-Job V2.1.5 版本分享 1. 概述 2. Maven模块 elastic-job-common-restful 3. Maven模块 elastic-job-cons ...
- BZOJ 2125: 最短路
2125: 最短路 Time Limit: 1 Sec Memory Limit: 259 MBSubmit: 756 Solved: 331[Submit][Status][Discuss] D ...