- [code C/C++]#include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define MAXN 2000000
- #include <iostream>
- #include <fstream>
- #include <string>
- int x[MAXN];
- using namespace std;
- void produceRandomFile(string& path,int m,int n);
- #define MASK 31
- int randint(int a, int b)
- { return a + (RAND_MAX * rand() + rand()) % (b + 1 - a);
- }
- int main(int argc, char *argv[])
- {
- int m = 1000000;
- int n = 10000000;
- int temp;
- int i, j;
- int value;
- string path = "D:\\program\\pearl\\test.txt";
- // produceRandomFile(path, m, n);
- ifstream in(path.c_str());
- int* a = new int[n>>5];
- for(i = 0; i < (n>>5); i++)
- {
- a[i] = 0;
- }
- while(!in.eof())
- {
- in >> temp;
- a[temp >> 5] = a[temp>>5] | (1 << (temp & MASK));
- }
- string outpath = "D:\\program\\pearl\\test1.txt";
- ofstream outfile(outpath.c_str());
- for(i = 0; i < (n>>5); i++ )
- {
- for(j = 0; j < 32; j++)
- {
- if(a[i] & (1 << j))
- {
- value = (i << 5) + j;
- outfile << value<< endl;
- }
- }
- }
- return 0;
- }
- void produceRandomFile(string& path,int m,int n)
- {
- ofstream out;
- out.open(path.c_str());
- int i, p, t;
- int* x= new int[n];
- srand((unsigned) time(NULL));
- for (i = 0; i < n; i++)
- x[i] = i;
- for (i = 0; i < m; i++) {
- p = randint(i, n-1);
- t = x[p]; x[p] = x[i]; x[i] = t;
- out << x[i] << endl;
- }
- out.close();
- }[code]
我写的编程珠玑第一章位图的实现,使用C++
最新推荐文章于 2023-06-17 12:30:59 发布