12014年组合数学实验报告.pdfVIP

  • 6
  • 0
  • 约1.62万字
  • 约 14页
  • 2017-05-21 发布于浙江
  • 举报
12014年组合数学实验报告

实验一 序数法生成全排列 源代码: import java.io.*; import java.util.ArrayList; class Xushufa{ public static void main(String args[]){ Xushufa Xushufa = null; int num = 0;//要排序的个数 boolean flag = true;//标志位,如果用户输入的待排序个数不合法,该值 一直为true ArrayListString strs = new ArrayListString(); while(flag){ try{ num = Integer.parseInt(readDataFromConsole(请输入待排序 的个数:)); flag = false; }catch(Exception e){ System.out.println(请输入整数.); } } for(int i = 1; i = num; i ++){ strs.add(readDataFromConsole(请输入第 + i + 个字符: )); } Xushufa = new Xushufa(strs.toArray(new String[]{})); System.out.println(排列后的数据为:); Xushufa.sort(); } private String[] str = null; public Xushufa(String[] s){ this.str = s; } //从控制台读入数据 private static String readDataFromConsole(String prompt) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; try { System.out.print(prompt); str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } private void sort(){ int num=str.length; int[] n1 = new int[num -1]; int[] nss = new int[num]; String[] s = new String[num]; boolean flag = false; int x = 0; do{ if(x == 0){//第一遍初始化 for(int i = 0;i num - 1;i ++){ n1[i] = 0; } } else {//生成序数 for(int i = 0;i num - 1;i ++){ if(n1[num - 2 - i] i + 1){ n1[num - 2 - i] ++; for(int j=num-1-i;jnum-1;j++){ n1[j] = 0; } break;

文档评论(0)

1亿VIP精品文档

相关文档