- package my.lottery.groups;
- /*************
- * 枚举出两种彩票,一种36选7,一种排列三.
- * 都有一个方法,可以获取彩票的所有球.
- */
- public enum Lottery{
-
- L_36(36) {
- public String[] getBalls() {
-
- String[] balls = new String[36];
- for (int i = 1; i <= count; i++) {
-
- if (i < 10)
- balls[i - 1] = "0" + i;
- else
- balls[i - 1] = i + "";
- }
- return balls;
- }
- },
- L_3(10) {
- public String[] getBalls() {
-
- String[] balls = new String[10];
- for (int i = 0; i < count; i++) {
- balls[i] = "0" + i;
- }
- return balls;
- }
- };
-
- protected int count;
-
- Lottery(int count) {
- this.count = count;
- }
-
- public abstract String[] getBalls();
- }
复制代码- package my.lottery.groups;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Properties;
- /**
- * 彩票36选7,计算出36选7的所有组合并把他们保存在文本文件中,每个文件保存60万组.
- */
- public class Lottery_1_36 {
-
- private static final String SEPARATOR = System.getProperty("line.separator");
-
- public static void main(String[] args) throws IOException {
-
- // 获取36选7的所有球。
- Lottery l_36 = Lottery.L_36;
- String[] balls = l_36.getBalls();
-
- //用于把组合储存到文件的流
- File dir = new File("f:\\彩票");
- if(!dir.exists()) dir.mkdir();
- BufferedWriter bufw = null;
-
- // 使用组合类获取36选7的所有组合,但是因为36选7的特殊情况,还要把获取到的每一个组合进行转换
- Groups<String> groups = new Groups<String>(balls, 7);
-
- // 控制每个文件保存的组合个数的一些变量
- int start = 0;
- int size = 100000;
- int fileCount = 1;
-
- over:
- while(true){ // 把组合储存到文件中每个文件保存60万组.
-
- File file = new File(dir,"36选7的所有组合"+fileCount+".txt");
- bufw = new BufferedWriter(new FileWriter(file));
-
- fileCount++;
-
- int end = start + size;
- boolean b = true;
-
- while(b){
-
- if(!groups.hasNext()) break over;
-
- //获取1-36的下一个组
- ArrayList<String> group = groups.nextGroup();
-
- //把1-36的组转成36选7的组
- StringBuilder group_1_36 = GroupTo36(group," ");
-
- //写入文件
- bufw.write(group_1_36.toString());
- bufw.flush();
-
- bufw.newLine();
-
- start++;
- if(start>end) b=false;
- }
- }
-
- // 用于把计算信息储存到文件的流
- File file2 = new File(dir,"计算信息.txt");
- FileWriter bufw2 = new FileWriter(file2,true);
-
- int mycount = groups.mycount*6;
- int govCount = groups.getGovCount()*6;
-
- Properties pro = new Properties();
-
- pro.setProperty("GOVCOUNT-----", govCount+"");
- pro.setProperty("MYCOUNT------", mycount+"");
- pro.setProperty("FILECOUNT----", (fileCount-1)+"");
- pro.setProperty("INFO---------", groups.getInfo());
- pro.store(bufw2, "---- ---- -- ---- --- --- ----- ---- ---- ---- ---");
-
- bufw2.close();
- bufw.close();
- }
- /**
- * 把从组合类返回的组合重新排列,并把每一个组合转换成字符串,保存到list集合中
- * 为什么要重新排列?
- * 因为36选7的特殊情况{1,2,3,4,5,6,7}还有六种组合
- * {1,2,3,4,5,7,6}
- * {1,2,3,4,6,7,5}
- * {1,2,3,5,6,7,4}
- * ...
- * {2,3,4,5,6,7,1}
- * 也就是元素互换....
- * @param group
- * @param sign 分隔符
- * @return 返回7组彩票
- */
- public static StringBuilder GroupTo36(ArrayList<String> group,String sign) {
-
- StringBuilder sb = new StringBuilder();
-
- //把组合{1,2,3,4,5,6,7}按指定格式添加到list集合中
- for (String element : group) {
- sb.append(element + sign);
- }
- sb.append(SEPARATOR);
- int lastIndex = group.size() - 1;
-
- //元素互换得到其他的六种组合并添加到list集合中
- for (int i = 1; i <= lastIndex; i++) {
-
- String lastelement = group.get(lastIndex);
- group.set(lastIndex, group.get(lastIndex-i));
- group.set(lastIndex-i, lastelement);
-
- for (String ii : group) {
- sb.append(ii + sign);
- }
- sb.append(SEPARATOR);
- }
- return sb;
- }
- }
复制代码
|
|