彩票优选号码问题

来源:互联网 发布:作业答案软件 编辑:程序博客网 时间:2024/04/29 06:20
 

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)

  1. #include "头文件.h"
  2. //记录字符串结构体
  3. typedef struct  str_long
  4. {
  5.     string  str ;//字符串
  6.     long    digit ;//字符串转化的数字
  7. } str_long ;
  8. //记录相同字符串结构体
  9. typedef struct  common_string
  10. {
  11.     str_long    str_long ;//相同字符串
  12.     int         position[2] ;//相同字符串在各集合的位置
  13. } common_string ;
  14. //随机初始化字符串结构体 temp 
  15. void    Random_str(str_long&    temp)
  16. {
  17.     string  str[21] = { "01""02""03""04","05","06","07","08","09","10","11",
  18.         "12","13","14","15","16","17","18","19","20","21" } ; 
  19.     vector<int> temp_vec ;
  20.     int     num ;
  21.     bool    random_same ;
  22.     
  23.     for(int i=0 ;i<5 ;i++)
  24.     {
  25.         do
  26.         {
  27.             random_same = false ;
  28.             num = rand()%21 ;
  29.             for(int j=0 ;j<temp_vec.size() ;j++)
  30.                 if( num==temp_vec[j] )
  31.                 {
  32.                     random_same = true ;
  33.                     break ;
  34.                 }
  35.         }while(random_same==true) ;
  36.     
  37.         temp.str += str[num] ;
  38.         temp_vec.push_back(num) ;
  39.     }
  40.     temp.digit = atol(temp.str.c_str()) ;   
  41. }
  42. //将str_l折半插入到有序向量str_vec、、、、、
  43. void    Select_insert(str_long str_l ,vector<str_long>& str_vec)
  44. {
  45.     int     begin = 0 , end = str_vec.size()-1 ;
  46.     int     position = (begin+end)/2 ;
  47.     bool    select_subarea = true ;//true表示选择前区,false表示选择后区
  48.     //str_long temp  = str_vec[position/2];
  49.     if(begin == end)
  50.     {
  51.         if(str_l.digit > str_vec[position].digit)
  52.             position = 1 ;
  53.         else
  54.             position = 0 ;
  55.         str_vec.insert(str_vec.begin()+position,str_l) ;
  56.         return ;
  57.     }
  58.     do
  59.     {
  60.         if(str_l.digit==str_vec[position].digit)
  61.         {
  62.             break ;
  63.         }
  64.             
  65.         if(str_l.digit > str_vec[position].digit)
  66.         {
  67.             select_subarea = true ;
  68.             begin = position+1 ;
  69.             position = (begin+end)/2 ;
  70.         }
  71.         else
  72.         {
  73.             select_subarea = false ;
  74.             end = position-1 ;
  75.             position = (begin+end)/2 ;
  76.         }       
  77.     }while(begin <= end ) ;
  78.     if(begin > end )
  79.     {
  80.         if(select_subarea==true )
  81.             str_vec.insert(str_vec.begin()+begin,str_l) ;
  82.         else
  83.             str_vec.insert(str_vec.begin()+end+1,str_l) ;//!!!注意end+1!!!!
  84.     }
  85.     else
  86.         str_vec.insert(str_vec.begin()+position,str_l) ;
  87. }
  88. //保存初始化字符数据str_vec(字符串向量)到文件filename
  89. void    Save_str(const vector<str_long>& str_vec ,string filename)
  90. {
  91.     ofstream    out_stream ;
  92.     out_stream.open(filename.c_str() ) ;// ,ios::app
  93.     for(int i=0 ;i<str_vec.size() ;i++)
  94.     {
  95.         out_stream << i << "    " << str_vec[i].str << "    " <<str_vec[i].digit <<endl ;
  96.     }
  97.     out_stream << endl ;
  98.     out_stream.close() ;
  99. }
  100. //初始化字符数据,并把它存到向量str_vec
  101. void    Init_string(vector<str_long>& str_vec ,long max)
  102. {
  103.     for(int i=0 ;i<max ;i++)
  104.     {
  105.         str_long    temp ;
  106.         Random_str( temp ) ;
  107.         if( str_vec.empty() )
  108.         {
  109.             temp.str = temp.str ;
  110.             temp.digit = temp.digit ;
  111.             str_vec.push_back(temp) ;
  112.             //system("pause") ;
  113.         }
  114.         else
  115.         {//折半插入
  116.             Select_insert(temp ,str_vec) ;          
  117.             //system("pause") ;
  118.         }
  119.     }
  120. }
  121. //折半查找:从向量str_vec查找和str_l相同的字符串,并存进common_str
  122. bool    Search_str(const vector<str_long>& str_vec ,const str_long& str_l ,
  123.                    common_string& common_str)
  124. {   
  125.     if(str_vec.empty())
  126.     {
  127.         cout << "/n 查找失败,字符串集合为空!/n" ;
  128.         return  false ;
  129.     }
  130.     int     begin = 0 , end = str_vec.size()-1 ;
  131.     int     position = (begin+end)/2 ;
  132.     bool    find_flag = false ;
  133.     do
  134.     {
  135.         if(str_l.digit==str_vec[position].digit)
  136.         {
  137.             find_flag = true ;
  138.             break ;
  139.         }
  140.             
  141.         if(str_l.digit > str_vec[position].digit)
  142.         {   
  143.             begin = position+1 ;
  144.             position = (begin+end)/2 ;
  145.         }
  146.         else
  147.         {
  148.             end = position-1 ;
  149.             position = (begin+end)/2 ;
  150.         }   
  151.     }while(begin <= end ) ;
  152.     if(find_flag==true)
  153.     {
  154.         common_str.str_long = str_vec[position] ;
  155.         common_str.position[1] = position ;
  156.         return  true ;
  157.     }
  158.     return  false ;
  159. }
  160. //找出集合(向量)A,B相同的字符串存进向量common_str_vec
  161. bool    Find_common_str(const vector<str_long>& A ,const vector<str_long>& B ,
  162.                         vector<common_string>& common_str_vec )
  163. {   
  164.     bool        same_exist = false ;
  165.     for(int i=0 ;i<A.size() ;i++)
  166.     {
  167.         common_string   common_str ;
  168.         //折半查找:
  169.         if( Search_str( B ,A[i] ,common_str) )
  170.         {
  171.             common_str.position[0] = i ;
  172.             common_str_vec.push_back(common_str) ;
  173.             same_exist = true ;         
  174.         }
  175.     }
  176.     return  same_exist ;
  177. }
  178. //保存相同字符串到文件filename
  179. bool    Save_common_str(const vector<common_string>& common_str_vec ,string filename)
  180. {
  181.     ofstream    out_stream ;
  182.     out_stream.open(filename.c_str() ) ;// ,ios::app
  183.     if(common_str_vec.empty())
  184.     {
  185.         out_stream << "=/n  查找失败,字符串集合(A ,B)没有相同元素!/n" ;
  186.         return  false ;
  187.     }   
  188.     for(int i=0 ;i<common_str_vec.size() ;i++)
  189.     {
  190.         out_stream << i << "    " << common_str_vec[i].str_long.str << "    " 
  191.                    << common_str_vec[i].str_long.digit << " " << common_str_vec[i].position[0]
  192.                    << " "<< common_str_vec[i].position[1]<<endl ;
  193.     }
  194.     out_stream << endl ;
  195.     out_stream.close() ;
  196.     return  true ;
  197. }
  198. void    main()
  199. {
  200.     time_t t;
  201.     srand((unsigned) time(&t));
  202.     //或:srand((unsigned) time(0));//只能初始化一次!!!!
  203.     vector<str_long>    A_str_vec ,B_str_vec  ;
  204.     vector<common_string>       common_str_vec ;
  205.     //字符串数量
  206.     long    max = 5000 ;
  207.     cout << "       !!!数据初始化中,请耐心等待!!!/n/n/n/n"  << endl ;
  208.     Init_string(A_str_vec ,max)  ;
  209.     cout << "向量A_str_vec长度为:" << A_str_vec.size() <<"   初始化成功!/n/n" << endl ;
  210.     Init_string(B_str_vec ,max) ;
  211.     cout << "向量B_str_vec长度为:" << B_str_vec.size() <<"   初始化成功!/n/n" << endl ;
  212.     Save_str(A_str_vec ,"A_str_vec.txt") ;  
  213.     Save_str(B_str_vec ,"B_str_vec.txt") ;
  214.     
  215.     
  216.     Find_common_str(A_str_vec ,B_str_vec ,common_str_vec) ;
  217.     Save_common_str(common_str_vec ,"common_str_vec.txt") ;
  218.     cout << "       查找集合(A,B)相同字符串完毕!请查看、、、/n/n/n" ;
  219.     system("common_str_vec.txt") ;
  220.     getch() ;
  221.     return ;
  222. }

 

原创粉丝点击