w 彩票问题:目前一种彩票模式是21选择5,购买彩票时候选择一串数字,例如:
02 05 08 16 21
w 现在有两个关于数字字符串的集合,找出它们中相同的字符串
w 随机生成这两个字符串集合,并且每个集合里字符串的个数不少于5000
//记录字符串结构体
typedef struct str_long
{
string str ;//字符串
long digit ;//字符串转化的数字
} str_long ;
//记录相同字符串结构体
typedef struct common_string
{
str_long str_long ;//相同字符串
int position[2] ;//相同字符串在各集合的位置
} common_string ;
//随机初始化字符串结构体 temp
void Random_str(str_long& temp)
//将str_l折半插入到有序向量str_vec、、、、、
void Select_insert(str_long str_l ,vector<str_long>& str_vec)
//保存初始化字符数据str_vec(字符串向量)到文件filename
void Save_str(const vector<str_long>& str_vec ,string filename)
//初始化字符数据,并把它存到向量str_vec
void Init_string(vector<str_long>& str_vec ,long max)
//折半查找:从向量str_vec查找和str_l相同的字符串,并存进common_str
bool Search_str(const vector<str_long>& str_vec ,const str_long& str_l ,
common_string& common_str)
//找出集合(向量)A,B相同的字符串存进向量common_str_vec
bool Find_common_str(const vector<str_long>& A ,const vector<str_long>& B ,
vector<common_string>& common_str_vec )
//保存相同字符串到文件filename
bool Save_common_str(const vector<common_string>& common_str_vec ,string filename)
- #include "头文件.h"
- //记录字符串结构体
- typedef struct str_long
- {
- string str ;//字符串
- long digit ;//字符串转化的数字
- } str_long ;
- //记录相同字符串结构体
- typedef struct common_string
- {
- str_long str_long ;//相同字符串
- int position[2] ;//相同字符串在各集合的位置
- } common_string ;
- //随机初始化字符串结构体 temp
- void Random_str(str_long& temp)
- {
- string str[21] = { "01", "02", "03", "04","05","06","07","08","09","10","11",
- "12","13","14","15","16","17","18","19","20","21" } ;
- vector<int> temp_vec ;
- int num ;
- bool random_same ;
- for(int i=0 ;i<5 ;i++)
- {
- do
- {
- random_same = false ;
- num = rand()%21 ;
- for(int j=0 ;j<temp_vec.size() ;j++)
- if( num==temp_vec[j] )
- {
- random_same = true ;
- break ;
- }
- }while(random_same==true) ;
- temp.str += str[num] ;
- temp_vec.push_back(num) ;
- }
- temp.digit = atol(temp.str.c_str()) ;
- }
- //将str_l折半插入到有序向量str_vec、、、、、
- void Select_insert(str_long str_l ,vector<str_long>& str_vec)
- {
- int begin = 0 , end = str_vec.size()-1 ;
- int position = (begin+end)/2 ;
- bool select_subarea = true ;//true表示选择前区,false表示选择后区
- //str_long temp = str_vec[position/2];
- if(begin == end)
- {
- if(str_l.digit > str_vec[position].digit)
- position = 1 ;
- else
- position = 0 ;
- str_vec.insert(str_vec.begin()+position,str_l) ;
- return ;
- }
- do
- {
- if(str_l.digit==str_vec[position].digit)
- {
- break ;
- }
- if(str_l.digit > str_vec[position].digit)
- {
- select_subarea = true ;
- begin = position+1 ;
- position = (begin+end)/2 ;
- }
- else
- {
- select_subarea = false ;
- end = position-1 ;
- position = (begin+end)/2 ;
- }
- }while(begin <= end ) ;
- if(begin > end )
- {
- if(select_subarea==true )
- str_vec.insert(str_vec.begin()+begin,str_l) ;
- else
- str_vec.insert(str_vec.begin()+end+1,str_l) ;//!!!注意end+1!!!!
- }
- else
- str_vec.insert(str_vec.begin()+position,str_l) ;
- }
- //保存初始化字符数据str_vec(字符串向量)到文件filename
- void Save_str(const vector<str_long>& str_vec ,string filename)
- {
- ofstream out_stream ;
- out_stream.open(filename.c_str() ) ;// ,ios::app
- for(int i=0 ;i<str_vec.size() ;i++)
- {
- out_stream << i << " " << str_vec[i].str << " " <<str_vec[i].digit <<endl ;
- }
- out_stream << endl ;
- out_stream.close() ;
- }
- //初始化字符数据,并把它存到向量str_vec
- void Init_string(vector<str_long>& str_vec ,long max)
- {
- for(int i=0 ;i<max ;i++)
- {
- str_long temp ;
- Random_str( temp ) ;
- if( str_vec.empty() )
- {
- temp.str = temp.str ;
- temp.digit = temp.digit ;
- str_vec.push_back(temp) ;
- //system("pause") ;
- }
- else
- {//折半插入
- Select_insert(temp ,str_vec) ;
- //system("pause") ;
- }
- }
- }
- //折半查找:从向量str_vec查找和str_l相同的字符串,并存进common_str
- bool Search_str(const vector<str_long>& str_vec ,const str_long& str_l ,
- common_string& common_str)
- {
- if(str_vec.empty())
- {
- cout << "/n 查找失败,字符串集合为空!/n" ;
- return false ;
- }
- int begin = 0 , end = str_vec.size()-1 ;
- int position = (begin+end)/2 ;
- bool find_flag = false ;
- do
- {
- if(str_l.digit==str_vec[position].digit)
- {
- find_flag = true ;
- break ;
- }
- if(str_l.digit > str_vec[position].digit)
- {
- begin = position+1 ;
- position = (begin+end)/2 ;
- }
- else
- {
- end = position-1 ;
- position = (begin+end)/2 ;
- }
- }while(begin <= end ) ;
- if(find_flag==true)
- {
- common_str.str_long = str_vec[position] ;
- common_str.position[1] = position ;
- return true ;
- }
- return false ;
- }
- //找出集合(向量)A,B相同的字符串存进向量common_str_vec
- bool Find_common_str(const vector<str_long>& A ,const vector<str_long>& B ,
- vector<common_string>& common_str_vec )
- {
- bool same_exist = false ;
- for(int i=0 ;i<A.size() ;i++)
- {
- common_string common_str ;
- //折半查找:
- if( Search_str( B ,A[i] ,common_str) )
- {
- common_str.position[0] = i ;
- common_str_vec.push_back(common_str) ;
- same_exist = true ;
- }
- }
- return same_exist ;
- }
- //保存相同字符串到文件filename
- bool Save_common_str(const vector<common_string>& common_str_vec ,string filename)
- {
- ofstream out_stream ;
- out_stream.open(filename.c_str() ) ;// ,ios::app
- if(common_str_vec.empty())
- {
- out_stream << "=/n 查找失败,字符串集合(A ,B)没有相同元素!/n" ;
- return false ;
- }
- for(int i=0 ;i<common_str_vec.size() ;i++)
- {
- out_stream << i << " " << common_str_vec[i].str_long.str << " "
- << common_str_vec[i].str_long.digit << " " << common_str_vec[i].position[0]
- << " "<< common_str_vec[i].position[1]<<endl ;
- }
- out_stream << endl ;
- out_stream.close() ;
- return true ;
- }
- void main()
- {
- time_t t;
- srand((unsigned) time(&t));
- //或:srand((unsigned) time(0));//只能初始化一次!!!!
- vector<str_long> A_str_vec ,B_str_vec ;
- vector<common_string> common_str_vec ;
- //字符串数量
- long max = 5000 ;
- cout << " !!!数据初始化中,请耐心等待!!!/n/n/n/n" << endl ;
- Init_string(A_str_vec ,max) ;
- cout << "向量A_str_vec长度为:" << A_str_vec.size() <<" 初始化成功!/n/n" << endl ;
- Init_string(B_str_vec ,max) ;
- cout << "向量B_str_vec长度为:" << B_str_vec.size() <<" 初始化成功!/n/n" << endl ;
- Save_str(A_str_vec ,"A_str_vec.txt") ;
- Save_str(B_str_vec ,"B_str_vec.txt") ;
- Find_common_str(A_str_vec ,B_str_vec ,common_str_vec) ;
- Save_common_str(common_str_vec ,"common_str_vec.txt") ;
- cout << " 查找集合(A,B)相同字符串完毕!请查看、、、/n/n/n" ;
- system("common_str_vec.txt") ;
- getch() ;
- return ;
- }