由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - How to detect if a base 10 decimal can be represented exactly in base 2?
相关主题
Leetcode 最新题, 搞不懂A few glassdoor questions
a silly question狗狗食堂可以带家属吗?
参院讨论的Employment Based议案5个concern[ZT}一小段程序,请诸位牛帮忙找下bug,谢谢!
HR问猎头要我的五年BONUS%,是不是有戏?LRU cache 问题
请教Offer Negotiation!多谢!微软一道 智力题
问个RSU和其他benefit相关的税收问题bloomberg onsite 面经
问两道google题明天ONSITE攒人品,发面试知识点总结!!
Amazon的LRU设计题请问strcpy()和memcpy()的写法问题
相关话题的讨论汇总
话题: base话题: num话题: str
进入JobHunting版参与讨论
1 (共1页)
h**o
发帖数: 548
1
given 1/10, 1/3.。。问能否 represented exactly in base 2?
怎么做?
J**9
发帖数: 835
2
check whether it can be equal 1/2 + 1/4 + 1/8 + ...
see /// below
/**
* Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a
double, print
* the binary representation. If the number c*annot be represented
accurately in binary
* with at most 32 characters, print "ERROR."
* hkBitsDoubleBits(): 0.720000 = 0.101110000101000111101011100001
* hkBitsDoubleBits(): 0.687500 = 0.1011
* hkBitsDoubleBits(): 0.500000 = 0.1
*/
char *hkBitsDoubleBits(double num, char *str, int maxSize)
{
if (!str)
return NULL;
if (num>=1.0 || num<0.0)
{
strcpy(str, "ERROR");
return str;
}
char *p = str;
*p++ = '0';
*p++ = '.';
maxSize -= 2;
while((num>0.0000000000001) && (maxSize>0))
{
double temp = 2.0 * num;
if (temp>=1.0)
{
*p++ = '1';
num = temp - 1.0;
/// if temp == 1.0, then can be represented exactly
}
else
{
*p++ = '0';
num = temp;
}
maxSize--;
}
*p = '\0';
return str;
}
d**********x
发帖数: 4083
3
why don't you just check the denomanitor and see if it is 2^n?
for m/10^n, playing with gcd(m, 10^n) will give you the answer

【在 J**9 的大作中提到】
: check whether it can be equal 1/2 + 1/4 + 1/8 + ...
: see /// below
: /**
: * Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a
: double, print
: * the binary representation. If the number c*annot be represented
: accurately in binary
: * with at most 32 characters, print "ERROR."
: * hkBitsDoubleBits(): 0.720000 = 0.101110000101000111101011100001
: * hkBitsDoubleBits(): 0.687500 = 0.1011

1 (共1页)
进入JobHunting版参与讨论
相关主题
请问strcpy()和memcpy()的写法问题请教Offer Negotiation!多谢!
how to access a const char array in a function问个RSU和其他benefit相关的税收问题
问一道Google的题问两道google题
akamai电面面经,攒rpAmazon的LRU设计题
Leetcode 最新题, 搞不懂A few glassdoor questions
a silly question狗狗食堂可以带家属吗?
参院讨论的Employment Based议案5个concern[ZT}一小段程序,请诸位牛帮忙找下bug,谢谢!
HR问猎头要我的五年BONUS%,是不是有戏?LRU cache 问题
相关话题的讨论汇总
话题: base话题: num话题: str