• 小小算法


    import java.util.ArrayList;
    import java.util.Random;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    public class RandomTest {

     /**
      * @param args
      */
     public static void main(String[] args) {
      // TODO Auto-generated method stub
      String sample1[] = {"a","b","0","1","d","c","g","h","i","j","6","7","k","m","3","4","n"};
      String sample2[] = {"p","r","q","s","t","u","v","w","z","y","x","2","5","8","9","e","f"};
      FileWriter wr=null;
      PrintWriter wout=null;
      
      Random r1 = new Random();
      ArrayList al = new ArrayList();
      int page = 0 ;
      for(int i = 0 ; i<10000;)
      {
       if(i%500==0)
       {
        if(wout!=null)
        {
         wout.close();
         wout = null;
        }
        try {
         if(wr!=null)
         {
          wr.close();
          wr = null;
         }
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
        page++;
        try {
         wr=new FileWriter("rand_"+page+".txt");
        } catch (Exception e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
        }
       }
       wout=new PrintWriter(wr);
       StringBuffer sb1 = new StringBuffer();
       for(int k=0;k<6;k++)
       {
        sb1.append(sample1[r1.nextInt(17)]);
       }
       if(!al.contains(sb1.toString()))
       {
        al.add(sb1.toString());
        i++;
       }
       else
       {
        continue;
       }
       ArrayList b1 = new ArrayList();
       Random r2 = new Random();
       for(int j = 0 ; j<10000;)
       {
        StringBuffer sb = new StringBuffer();
        for(int p = 0 ;p<6;p++)
        {
         sb.append(sample2[r2.nextInt(17)]);
        }
        if(!b1.contains(sb.toString()))
        {
         b1.add(sb.toString());
         j++;
         //System.out.println(sb1.toString()+sb.toString());
         wout.println(sb1.toString()+sb.toString());
         wout.flush();
        }
       }
       b1.clear();
      }
      al.clear();
     }
    }

  • 相关阅读:
    计算器类(C++&JAVA——表达式转换、运算、模板公式)
    使用/\_ 打印正三角形 C/C++
    Triangle2D类(Java)
    让键盘输入不影响界面的动态效果(C++)
    Java 7.21 游戏:豆机(C++&Java)
    Java 7.35 游戏:猜字游戏(C++&Java)
    Java 8.9 游戏:井字游戏(C++&Java)
    JAVA 8.20 游戏:四子连(Java&C++)
    简易Java文本编译器(C++)
    PAT 1089 狼人杀-简单版(20 分)(代码+测试点分析)
  • 原文地址:https://www.cnblogs.com/syf/p/920877.html
Copyright © 2020-2023  润新知