- 论坛徽章:
- 0
|
写了一个程序来实现
根据win_hate的意见写了一个生成号码的程序,效率一般,功能差不多可以实现。
ID_MAX_LEN:号码长度
VEC_SIZE:号码数量
更改这两个值可以生成不同的长度和数量的不重复的号码。
兄弟们看看,多提意见。
- #include <iostream>
- #include <vector>
- #include <stdlib.h>
- #include <string>
- #include <fstream>
- #include <algorithm>
- using namespace std;
- const int ID_MAX_LEN = 10;
- const int VEC_SIZE = 100000;
- int main(int argc, char *argv[])
- {
- int i,j;
- vector<string> vec_id;
- //set rand seed
- srand((int)time(0));
- //genarate rand vector
- for( int ix = 0; ix < VEC_SIZE; ix++ )
- {
- char id[ID_MAX_LEN+1];
- for( i = 0; i < ID_MAX_LEN; i++)
- {
- j = 0+(int)(10.0*rand()/(RAND_MAX+1.0));
- id[i] = 48 + j;
- }
- id[ID_MAX_LEN] = '\0';
- string s = id;
- vec_id.push_back(s);
- }
-
- //sort and erase the repeated elements
- sort(vec_id.begin(), vec_id.end());
- vector<string>::iterator new_end = unique( vec_id.begin(), vec_id.end() );
- vec_id.erase( new_end, vec_id.end() );
- //insert elements to replace the deleted elements
- while( vec_id.size() < VEC_SIZE )
- {
- while( vec_id.size() < VEC_SIZE )
- {
- char id[ID_MAX_LEN+1];
- for( i = 0; i < ID_MAX_LEN; i++)
- {
- j = 0+(int)(10.0*rand()/(RAND_MAX+1.0));
- id[i] = 48 + j;
- }
- id[ID_MAX_LEN] = '\0';
- string s = id;
- vec_id.push_back(s);
- }
- //sort and erase the repeated element
- sort(vec_id.begin(), vec_id.end());
- vector<string>::iterator new_end = unique( vec_id.begin(), vec_id.end() );
- vec_id.erase( new_end, vec_id.end() );
- }
-
- //write to the file
- ofstream outfile( "id.txt" );
- for( vector<string>::iterator iter = vec_id.begin(); iter != vec_id.end(); iter++ )
- {
- //cout << *iter << endl;
- outfile << *iter << endl;
- }
- outfile.close();
-
- system("PAUSE");
- return 0;
- }
复制代码 |
|