-4

我被要求制作一个字节文件。我得到了数组字节 a[] = {97, 98, 99, 100, 101, 102} ,现在我必须从 a[] 数组中创建字节文件。但我有限制:

  1. 文件的数据必须是随机的(来自 a[])
  2. 必须几乎同时在文件中检测到所有字节。

    公共类 WriteByteFile { byte[] a = {97, 98, 99, 100, 101, 102};

    最终字符串文件=“文件”;

    /* 打开文件 */ BufferedOutputStream out = null; 尝试 { out = new BufferedOutputStream (new BufferedOutputStream(file)); } catch (FileNotFoundException e1) { System.err.println("无法打开文件:" + file1 + ":" + e1.getMessage()); System.exit(1); }

    /* 写入文件 */ try { for (int b = 0; b < 1650; b++) { out.write(//这里我必须从 a[] 中获取随机元素,但在接下来的 5 次中不能再获得它) ; }

4

2 回答 2

1

1.the data of the file must be random (from the a[])

2.all the bytes must be detected in the file almost the same times.

这两条线是矛盾的。如果您的文件中必须存在所有字符same no of times,那么您从数组中选择的字符不是随机的。

于 2012-12-15T09:44:29.850 回答
0

必须几乎同时在文件中检测到所有字节。

这里的关键是“几乎”。这里不需要精确等价,只需要统计等价。

如果您随机选择每个字节,独立于其他字节,您将满足这一点。统计将完成其余的工作。如果你继续掷骰子,你会得到几乎相同的 1-6 次,但是没有机制可以保证完全等价(事实上,这里不太可能完全等价)。

于 2012-12-15T09:52:47.817 回答