黑白程式

黑白程式

导航

快速生成10亿随机不重复数据代码(转)

#include     <cstdlib>   
#include     
<iostream>  
#include     
<windows.h>   

using   namespace   std;


char   map2char[]={'1','2','3','4','5','6','7','8','b','c','d','e','f','g','h','i',
'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y'}
;

void    getbit( unsigned e, char *s, int offset ){
    
int i=17;
    
while( i>=0 ){
        s[
18*offset+i]=((e>>i)&1);
        
--i;
    }
   
}


int   main(int   argc,   char   *argv[])   
{      
    
    srand(GetTickCount());
    
char bit[90], S[90];
    
forint i=0; i<sizeof(bit); ++i ) bit[i]=0, S[i]=0;
    unsigned  x, T;
    
forint k=0; k<5++k ){
        
do
        x
=rand();
        
while( x==0 );
        getbit( x, S, k);
        getbit( x, bit, k );
    }

    unsigned head
=89;
    unsigned  j
=0, time=GetTickCount(), Max=1000000;
    
do
        
++j;
        T
=(bit[head]&1)^(bit[(head-83)%90]&1)^(bit[(head-84)%90]&1)^(bit[(head-86)%90]&1)^(bit[(head-89)%90]&1);
        bit[head]
=T;
        head
=(head+1)%90;
        
forint index=0; index<18++index ){
            
int num=0;
            
forint j=0; j<5++j )
                num
+=(bit[(head+5*index+j)%90]==0)? 0 : (1<<j);            
            cout
<<map2char[num];        
        }

        cout
<<endl;        
    }
while( j<Max );
    cout
<<"time: "<<GetTickCount()-time<<" ms!\n";      
    
    system(
"PAUSE");   
    
return   EXIT_SUCCESS;   
}
 

posted on 2009-05-08 15:42  黑白程式  阅读(330)  评论(0编辑  收藏  举报