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);
      }
    }

  • 相关阅读:
    Jsp数据交互
    java生成验证码
    http状态码
    Java设计模式之单例
    QQ数据库管理
    Mysql中的存储过程,事物
    mysql中约束的添加,修改,与删除
    mysql中定义存储过程
    Mysql中的子查询等操作
    数据库的简单操作
  • 原文地址:https://www.cnblogs.com/-rainbow-/p/7410888.html
Copyright © 2011-2022 走看看