vb.net太慢>_< c++重写一遍, 心情爽! [cpp] #include<fcntl.h> #include<unistd.h> #include<cstring> #include<iostream> using std::cout; using std::endl; typedef unsigned long long uint64; #define UINT32_MAX 0x100000000L void writeBool(int fd, uint64 pos, bool b) { if (pos<0 || pos>UINT32_MAX) { return; } char buf; lseek(fd, pos>>3, SEEK_SET); size_t len=read(fd, &buf, 1); //if not set yet if (len<=0) buf=0; if (b) buf |= 1<<(pos % 8); else buf &= ~(1<<(pos % 8)); lseek(fd, pos>>3, SEEK_SET); write(fd, &buf, 1); } bool readBool(int fd, uint64 pos) { if (pos<0 || pos>UINT32_MAX) { return false; } char buf; lseek(fd, pos>>3, SEEK_SET); size_t len=read(fd, &buf, 1); //if not set yet if (len<=0) buf=0; return (buf>>(pos % 8))&1; } void writeFF(int fd, size_t block_size, size_t count) { char buf[block_size]; memset(buf, 0xFF, sizeof(buf)); lseek(fd, 0, SEEK_SET); for(int i=0;i<count;++i) write(fd, buf, block_size); } int main() { int fd=open("./pt.bin", O_RDWR | O_CREAT, 0644); //init writeFF(fd, 4096, UINT32_MAX/8/4096); cout<<"init done"<<endl; writeBool(fd, 0, false); writeBool(fd, 1, false); for(int i=2;i<=UINT32_MAX;++i) { if (readBool(fd, i) && ((UINT32_MAX/i)>i) ) { for(int j=i*i;j<=UINT32_MAX;j+=i) { writeBool(fd, j, false); } } } cout<<readBool(fd, 0)<<" "<<readBool(fd, 2)<<endl; close(fd); return 0; } [/cpp]
发表评论?
4 条评论。
发表评论
本文链接:https://twd2.me/archives/4479
这玛尼难道是Linux C++程序?看上去好高端
o.o 是在Linux下撰写的0 0
这是啥高亮,居然可以点击编辑
为啥我这里不能编辑