首先生成100w随机数,控制最大数
<?php $i = 0; do{ $i++; $num = rand(0, 999999); echo $num." "; }while($i < 1000000);
直接上代码
#include <stdio.h> #include <string.h> #define MAX 1000000 unsigned char set_bit(unsigned char x, int n){ x = x | 1 << n; return x; } unsigned char clr_bit(unsigned char x, int n){ x = x & ~(1 << n); return x; } int get_bit(unsigned char x, int n){ int ret = (x >> n) & 1; return ret; } int main(){ unsigned char x[MAX / 8 + 1]; memset(x, 0, sizeof(x)); FILE *fp = fopen("txt", "r"); char buf[8]; int i, idx, n; while(!feof(fp) && fgets(buf, sizeof(buf), fp)){ sscanf(buf, "%d", &i); idx = i / 8; n = i % 8; x[idx] = set_bit(x[idx], n); } fclose(fp); for(i = 0; i < MAX; i++){ idx = i / 8; n = i % 8; if(get_bit(x[idx], n)){ printf("%d ", i); } } }
运行效果