Find an integer not among four billion given ones

本文深入探讨了如何在内存限制条件下,寻找四亿整数集合中的缺失整数,包括使用位图法解决大规模整数集合问题及内存不足时的优化策略。

It is an interview question:

Given an input file with four billion integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1 GB memory. Follow up with what you would do if you have only 10 MB of memory.

My analysis:

文件的大小

The size of the file is 4×109×4 bytes = 16 GB.

做排序可以很好的解决问题,只要找到排序范围外的整数即可。但是有没有除了排序更好的方法呢?

We can do external sorting, thus we get to know the range of the integers. My question is what is the best way to detect the missing integer in the sorted big integer sets?

My understanding(after reading all answers):

假设我们处理的是32位的整数,那么一共有2^32 约为 4*109 个不同的整数。

Assuming we are talking about 32-bit integers. There are 2^32 = 4*109 distinct integers.

有足够内存时可以使用位图,用每一位来表示一个整数。

Case 1: we have 1 GB = 1 * 109 * 8 bits = 8 billion bits memory. Solution: if we use one bit representing one distinct integer, it is enough. we don't need sort. Implementation:

回顾一下读文件:

作为C++风格的文件读取方式
可以使用文件流类——fstream类
fstream类有两种子类
分别是用于读出文件的ifstream类
以及用于写入文件ofstream类

在使用是应加入引用 : #include <fstream>
注意该头文件使用std命名空间
还应该加入语句 :using namespace std;

使用的使用应该创建一个文件流对象
比如读入一个文件可以使用下列语句:
      ifstream File;
      char *FileName;
      char DataBuffer[128];
      /* 此处应设定文件名 */
      File.open(FileName);  //打开文件
      //open函数其实有三个参数,此处后两个使用默认值了,具体函数使用请见MSDN
      if(File)
      {  //文件打开成功
         // 此处加入对文件内容的处理
         while(!File.eof())
         {        //循环读入数据
                  File.read(DataBuffer,128);
                  /*对缓冲区中的读入数据进行操作*/
         }
      }
      else
      {  //文件打开失败
         /*进行错误处理*/
      }
      File.close();  //关闭文件
与上述代码类似
将内容写入文件需要创建一个ofstream对象

可以多看看MDSN
可以参考CPP标准函数库 

int radix = 8;
byte[] bitfield = new byte[0xffffffff/radix];
void F() throws FileNotFoundException{
    Scanner in = new Scanner(new FileReader("a.txt"));
    while(in.hasNextInt()){
        int n = in.nextInt();
        bitfield[n/radix] |= (1 << (n%radix));
    }

    for(int i = 0; i< bitfield.lenght; i++){
        for(int j =0; j<radix; j++){
            if( (bitfield[i] & (1<<j)) == 0) System.out.print(i*radix+j);
        }
    }
}

对从文件中读出的整数进行处理的函数:

These functions use the constants to set, clear and test the value of a bit:


#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 4000000000
int a[1 + N/BITSPERWORD];
一个整数a对32取模,即留下后5个bit:a&0x1F

void set(int i) {        a[i>>SHIFT] |=  (1<<(i & MASK)); }//将整数对应的位置为1
void clr(int i) {        a[i>>SHIFT] &= ~(1<<(i & MASK)); }
int  test(int i){ return a[i>>SHIFT] &   (1<<(i & MASK)); }//测试整数对应的位
内存不足时:

Case 2: 10 MB memory = 10 * 106 * 8 bits = 80 million bits

对于所有16bit的前缀,有65536种前缀,需要65536*4*8 = 2million bits。需要建立65536个桶。对于每一个桶,需要4bytes去存储所有可能的计数。因为最坏的情况是所有的数字都用一个桶计数。

Solution: For all possible 16-bit prefixes, there are 2^16 number of
integers = 65536, we need 2^16 * 4 * 8 = 2 million bits. We need build
65536 buckets. For each bucket, we need 4 bytes holding all possibilities because
 the worst case is all the 4 billion integers belong to the same bucket.

方法:增加计数器的值,读取计数器的值。
桌上有十个苹果,要把这十个苹果放到九个抽屉里,无论怎样放,我们会发现至少会有一个抽屉里面放两个苹果。这一现象就是我们所说的“抽屉原理”。 
遍历文件,将每个整数的前16位对应的编号的桶中的计数器加1。
遍历每个桶,找出第一个计数器值小于65536的桶,记录此桶的编号,就是数组的下标,也就是缺失数字的前16bit了。
再次遍历数据文件,将数据的前16bit为上一步找出的数放入新的桶中。新的桶的编号为所找出的数的后16bit。
最后找出计数器为0的桶,桶的编号就为所缺的数的后16bit。
.......
或者最后一步可以使用bitmap,将拥有相同前16bit的数字的后16bit对应的数字用bitmap计数。

step1: Build the counter of each bucket through the first pass through the file.
step2: Scan the buckets, find the first one who has less than 65536 hit.
step3: Build new buckets whose high 16-bit prefixes are we found in step2
through second pass of the file
step4: Scan the buckets built in step3, find the first bucket which doesnt
have a hit.

The code is very similar to above one.

Conclusion: We decrease memory through increasing file pass.

http://stackoverflow.com/questions/7153659/find-an-integer-not-among-four-billion-given-ones
基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值