题目:此题缘于《编程珠玑》,一个文件包含最多N个正整数,每一个都小于
N,每一个数都不会出现两次以上。将这个文件排序。
珠玑上的解法:
/*phase 1: initialize set to empty*/
for i = [0, n)
bit[i] = 0
/*phase 2: insert present elements into the set*/
for each i in the input file
bit[i] = 1
/*phase 3: write sorted output*/
for i = [0,n)
if bit[i] == 1
write i on the output file
下面的原代码包括下面几个问题的解决方案:
1. 给一个正整数数组,按序打印出来
2. 给出一个正整数数组,包含1到N的所有数,某一个数除外,
要你找出这个数。有两种解法,一种是用跟问题1同样的方法
用BITMAP,另外一种方法是用异或的功能
3. 给出1-N的所有正整数,要求其任意一个排列,且概率为1/n!.