zoukankan      html  css  js  c++  java
  • 算法题(分小组)

    问题:

    分小组
    9名运动员参加比赛,需要分3组进行预赛。
    有哪些分组的方案呢?
    我们标记运动员为 A,B,C,... I
    下面的程序列出了所有的分组方法。
    该程序的正常输出为:
    ABC DEF GHI
    ABC DEG FHI
    ABC DEH FGI
    ABC DEI FGH
    ABC DFG EHI
    ABC DFH EGI

    代码:

    public class Group {
      private static int count;
      public static void main(String[] args) {
        int[] a = new int[9];
        a[0] = 1;

        for (int i = 1; i < a.length; i++) {
          a[i] = 1;
          for (int j = i+1; j < a.length; j++) {
            a[j] = 1;
            secondGroup("A"+(char)(i+'A')+(char)(j+'A'),a);
            a[j] = 0;
          }
          a[i] = 0;
        }
      }

      private static void secondGroup(String s, int[] a) {
        for (int i = 1; i < a.length; i++) {
          if (a[i]==1) {
            continue;
          }
          a[i] = 1;
          for (int j = i+1; j < a.length; j++) {
            if (a[j]==1) {
              continue;
            }
            a[j] = 1;
            for (int j2 = j+1; j2 < a.length; j2++) {
              if (a[j2]==1) {
                continue;
              }
              a[j2] = 1;
              thirdGroup(s+(char)(i+'A')+(char)(j+'A')+(char)(j2+'A'),a);
              a[j2] = 0;
            }
            a[j] = 0;
          }
          a[i] = 0;
        }
      }

      private static void thirdGroup(String s, int[] a) {
        for (int i = 1; i < a.length; i++) {
          if (a[i]==0) {
            s += (char)(i+'A');
          }
        }
        System.out.println(s);
      }
    }

  • 相关阅读:
    编写安全检测脚本
    编写监控脚本
    编写一键部署软件脚本
    awk扩展应用
    sed基本用法
    字符串截取及切割,正则表达式,expect预期交互
    For,while,case,shell循环结构
    mybatis使用associaton进行分步查询
    mybatis中封装结果集常见示例
    Mybatis获取数据库自增主键
  • 原文地址:https://www.cnblogs.com/-rainbow-/p/7410888.html
Copyright © 2011-2022 走看看