我写的编程珠玑第一章位图的实现,使用C++

  1. [code C/C++]#include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define MAXN 2000000
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. int x[MAXN];
  9. using namespace std;


  10. void produceRandomFile(string& path,int m,int n);


  11. #define MASK 31
  12. int randint(int a, int b)
  13. { return a + (RAND_MAX * rand() + rand()) % (b + 1 - a);
  14. }


  15. int main(int argc, char *argv[])
  16. {
  17. int m = 1000000;
  18. int n = 10000000;
  19. int temp;
  20. int i, j;
  21. int value;


  22. string path = "D:\\program\\pearl\\test.txt";
  23. // produceRandomFile(path, m, n);

  24. ifstream in(path.c_str());


  25. int* a = new int[n>>5];
  26. for(i = 0; i < (n>>5); i++)
  27. {
  28. a[i] = 0;
  29. }
  30. while(!in.eof()) 
  31. {
  32. in >> temp;
  33. a[temp >> 5] = a[temp>>5] | (1 << (temp & MASK));
  34. }

  35. string outpath = "D:\\program\\pearl\\test1.txt";
  36. ofstream outfile(outpath.c_str());
  37. for(i = 0; i < (n>>5); i++ )
  38. {
  39. for(j = 0; j < 32; j++)
  40. {
  41. if(a[i] & (1 << j))
  42. {
  43. value = (i << 5) + j;
  44. outfile << value<< endl;
  45. }
  46. }
  47. }


  48. return 0;
  49. }


  50. void produceRandomFile(string& path,int m,int n)
  51. {
  52. ofstream out;
  53. out.open(path.c_str());


  54. int i, p, t;
  55. int* x= new int[n];
  56. srand((unsigned) time(NULL));
  57. for (i = 0; i < n; i++)
  58. x[i] = i;
  59. for (i = 0; i < m; i++) {
  60. p = randint(i, n-1);
  61. t = x[p]; x[p] = x[i]; x[i] = t;
  62. out << x[i] << endl;
  63. }
  64. out.close();
  65. }[code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值