A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 354620815 中级黑马   /  2014-10-3 15:25  /  1009 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package my.lottery.groups;
  2. /*************
  3. *        枚举出两种彩票,一种36选7,一种排列三.
  4. *        都有一个方法,可以获取彩票的所有球.
  5. */
  6. public enum Lottery{
  7.        
  8.         L_36(36) {
  9.                 public String[] getBalls() {
  10.                        
  11.                         String[] balls = new String[36];
  12.                         for (int i = 1; i <= count; i++) {
  13.                                
  14.                                 if (i < 10)
  15.                                         balls[i - 1] = "0" + i;
  16.                                 else
  17.                                         balls[i - 1] = i + "";
  18.                         }
  19.                         return balls;                       
  20.                 }               
  21.         },
  22.         L_3(10) {
  23.                 public String[] getBalls() {
  24.                        
  25.                         String[] balls = new String[10];
  26.                         for (int i = 0; i < count; i++) {

  27.                                 balls[i] = "0" + i;
  28.                         }
  29.                         return balls;
  30.                 }
  31.         };       
  32.        
  33.         protected int count;
  34.        
  35.         Lottery(int count) {
  36.                 this.count = count;
  37.         }
  38.        
  39.         public abstract String[] getBalls();
  40. }
复制代码
  1. package my.lottery.groups;

  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Properties;
  8. /**
  9. * 彩票36选7,计算出36选7的所有组合并把他们保存在文本文件中,每个文件保存60万组.
  10. */
  11. public class Lottery_1_36 {
  12.        
  13.         private static final String SEPARATOR = System.getProperty("line.separator");
  14.        
  15.         public static void main(String[] args) throws IOException {               
  16.                
  17.                 // 获取36选7的所有球。
  18.                 Lottery l_36 = Lottery.L_36;
  19.                 String[] balls = l_36.getBalls();                       
  20.                
  21.                 //用于把组合储存到文件的流
  22.                 File dir = new File("f:\\彩票");
  23.                 if(!dir.exists()) dir.mkdir();               
  24.                 BufferedWriter bufw = null;                                       
  25.                
  26.                 // 使用组合类获取36选7的所有组合,但是因为36选7的特殊情况,还要把获取到的每一个组合进行转换
  27.                 Groups<String> groups = new Groups<String>(balls, 7);                       
  28.                
  29.                 // 控制每个文件保存的组合个数的一些变量
  30.                 int start = 0;
  31.                 int size = 100000;
  32.                 int fileCount = 1;
  33.                
  34.                 over:
  35.                 while(true){ // 把组合储存到文件中每个文件保存60万组.                       
  36.                        
  37.                         File file = new File(dir,"36选7的所有组合"+fileCount+".txt");
  38.                         bufw = new BufferedWriter(new FileWriter(file));
  39.                        
  40.                         fileCount++;
  41.                        
  42.                         int end = start + size;
  43.                         boolean b = true;
  44.                        
  45.                         while(b){
  46.                                
  47.                                 if(!groups.hasNext()) break over;                               
  48.                                
  49.                                 //获取1-36的下一个组
  50.                                 ArrayList<String> group = groups.nextGroup();
  51.                                
  52.                                 //把1-36的组转成36选7的组
  53.                                 StringBuilder group_1_36 = GroupTo36(group," ");                       
  54.                                
  55.                                 //写入文件
  56.                                 bufw.write(group_1_36.toString());
  57.                                 bufw.flush();
  58.                                        
  59.                                 bufw.newLine();                               
  60.                                                        
  61.                                 start++;
  62.                                 if(start>end) b=false;                               
  63.                         }       
  64.                 }               
  65.                
  66.                 // 用于把计算信息储存到文件的流
  67.                 File file2 = new File(dir,"计算信息.txt");
  68.                 FileWriter bufw2 = new FileWriter(file2,true);
  69.                
  70.                 int mycount =  groups.mycount*6;
  71.                 int govCount =  groups.getGovCount()*6;
  72.                
  73.                 Properties pro = new Properties();
  74.                
  75.                 pro.setProperty("GOVCOUNT-----", govCount+"");
  76.                 pro.setProperty("MYCOUNT------", mycount+"");
  77.                 pro.setProperty("FILECOUNT----", (fileCount-1)+"");
  78.                 pro.setProperty("INFO---------", groups.getInfo());
  79.                 pro.store(bufw2, "---- ---- -- ---- --- --- ----- ---- ---- ---- ---");               
  80.                
  81.                 bufw2.close();
  82.                 bufw.close();
  83.         }
  84.         /**
  85.          * 把从组合类返回的组合重新排列,并把每一个组合转换成字符串,保存到list集合中
  86.          * 为什么要重新排列?
  87.          * 因为36选7的特殊情况{1,2,3,4,5,6,7}还有六种组合
  88.          * {1,2,3,4,5,7,6}
  89.          * {1,2,3,4,6,7,5}
  90.          * {1,2,3,5,6,7,4}
  91.          * ...
  92.          * {2,3,4,5,6,7,1}
  93.          * 也就是元素互换....
  94.          * @param group
  95.          * @param sign 分隔符
  96.          * @return        返回7组彩票
  97.          */
  98.         public static StringBuilder GroupTo36(ArrayList<String> group,String sign) {               
  99.                
  100.                 StringBuilder sb = new StringBuilder();
  101.                
  102.                 //把组合{1,2,3,4,5,6,7}按指定格式添加到list集合中
  103.                 for (String element : group) {
  104.                         sb.append(element + sign);
  105.                 }
  106.                 sb.append(SEPARATOR);

  107.                 int lastIndex = group.size() - 1;
  108.                
  109.                 //元素互换得到其他的六种组合并添加到list集合中
  110.                 for (int i = 1; i <= lastIndex; i++) {
  111.                        
  112.                         String lastelement = group.get(lastIndex);
  113.                         group.set(lastIndex, group.get(lastIndex-i));
  114.                         group.set(lastIndex-i, lastelement);
  115.                        
  116.                         for (String ii : group) {
  117.                                 sb.append(ii + sign);
  118.                         }                       
  119.                         sb.append(SEPARATOR);
  120.                 }
  121.                 return sb;
  122.         }
  123. }
复制代码




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马