我正在阅读算法第4版.我在阅读第3章搜索时遇到了一些问题.从成本汇总中,BinarySearchST的插入成本(在最坏的情况下为2N)比SequentialSearchST(在最坏的情况下为N)稍差.但使用VisualAccumulator(绘制图表)的FrequencyCounter测试显示
对于长度为8或更长的字,返回FrequencyCounter的put()操作的成本,我们看到,对于SequentialSearchST,每次操作的平均成本从2,246比较(加上数组访问)减少到BinarySearchST的484.
BinarySearchST的put()操作是否需要比SequentialSearchST更多的比较(加上数组访问)?
另一个问题,对于BinarySearchST,这本书说
命题B(续).在最坏的情况下,将一个新密钥插入一个大小为N的有序数组中使用~2N数组访问,因此在最坏的情况下将N个密钥插入一个最初为空的表使用~N 2个数组访问
当我查看BinarySearchST的代码时,我认为将一个新密钥插入一个大小为N的有序数组中会使用~4N数组访问.
public void put(Key key, Value val) {
if (key == null) throw new IllegalArgumentException("first argument to put() is null");
if (val == null) {
delete(key);
return;
}
int i = rank(key);
// key is already in table
if (i < n && keys[i].compareTo(key) == 0) {
vals[i] = val;
return;
}
// insert new key-value pair
if (n == keys.length) resize(2*keys.length);
for (int j = n; j > …Run Code Online (Sandbox Code Playgroud) IEEE 754浮点数是离散的。
public class MyTest2 {
public static void main(String[] args) {
//about 1.00000001490116119384765625E-1 in IEEE-754
float f = 0.1f;
//about 1.00000000000000005551115123126E-1 in IEEE-754
double d = 0.1d;
System.out.println(String.format("double 0.1= %.30f", d));
System.out.println(String.format("float 0.1 = %.15f", f));
System.out.println(d+"");
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅在 IdeOne.com 上实时运行的代码。在JDK8中运行,输出为
double 0.1= 0.100000000000000000000000000000
float 0.1 = 0.100000001490116
0.1
Run Code Online (Sandbox Code Playgroud)
浮点值按预期打印。我希望打印双精度值 0.1d 类似 1.000000000000000055511151231260。为什么它在小数部分打印全零?
如果我将双精度变量 d 转换为字符串,它会打印 0.1。
System.out.println(d+"");
Run Code Online (Sandbox Code Playgroud)
java如何将最接近的浮点值0.1d(大约为1.00000001490116119384765625E-1)转换为精确的0.1?
In chapter 4, section Compound Assignment of the book: C Programming: A Modern Approach, 2nd Edition, says:
Note that I've been careful not to say that
v += eis “equivalent” tov = v + e. One problem is operator precedence:i * = j + kisn't the same asi = i * j + k.
I write a program to compare i * = j + k with i = i * j + …