其他分享
首页 > 其他分享> > hj_三三对决分配对手

hj_三三对决分配对手

作者:互联网

import java.util.*;

/**
 * @author hj
 * @version 1.0
 * @Description: 两个球队比赛, 龙队 a,b,c三人,虎队 x,y,z三人,a不和x比 ,c不和x,z比,找出并打印三对选手的对决
 */
class HjBallGame {

    public static void main(String[] args) {
        System.out.println("-----------------");
        threeFor();
        System.out.println("-----------------");
        threeForOptimizing();
        System.out.println("-----------------");
        setContainer();
    }

    /**
     * 直接法.三重循环,简单明了一路判断
     */
    private static void threeFor() {
        final char x = 'x';
        final char z = 'z';
        for (char a = x; a <= z; a++) {
            for (char b = x; b <= z; b++) {
                for (char c = x; c <= z; c++) {
                    if (a != x && c != x && c != z && a != b && a != c && b != c) {
                        System.out.println("a---" + a);
                        System.out.println("b---" + b);
                        System.out.println("c---" + c);
                    }
                }
            }
        }
    }

    /**
     * 直接法.三重循环,稍微优化
     */
    private static void threeForOptimizing() {
        final char x = 'x';
        final char z = 'z';
        for (char a = x; a <= z; a++) {
            for (char b = x; b <= z; b++) {
                if (a != b) {
                    for (char c = x; c <= z; c++) {
                        if (c != a && c != b && c != x && c != z && a != x) {
                            System.out.println("a----PK----" + a);
                            System.out.println("b----PK----" + b);
                            System.out.println("c----PK----" + c);
                        }
                    }
                }
            }
        }
    }

    /**
     * 集合容器法
     */
    private static void setContainer() {
        //龙 虎 队
        char[] dragon = {'a', 'b', 'c'};
        char[] tiger = {'x', 'y', 'z'};
        Set<Character> setA = new HashSet<>();
        Set<Character> setB = new HashSet<>();
        Set<Character> setC = new HashSet<>();
        for (char d : dragon) {
            for (char t : tiger) {
                switch (d) {
                    case 'a':
                        if (t != 'x') {
                            setA.add(t);
                        }
                        break;
                    case 'b':
                        setB.add(t);
                        break;
                    case 'c':
                        if (t != 'x' && t != 'z') {
                            setC.add(t);
                        }
                        break;
                    default:
                        System.out.println("异常了");
                        break;
                }
            }
        }
        deWeight(setA, setB, setC);
        deWeight(setB, setC, setA);
        deWeight(setC, setA, setB);
        System.out.println("龙队 a --->>"+setA.iterator().next());
        System.out.println("龙队 b --->>"+setB.iterator().next());
        System.out.println("龙队 c --->>"+setC.iterator().next());
    }

    /**
     * 去重
     *
     * @param setA a的对手集
     * @param setB b的对手集
     * @param setC c的对手集
     */
    private static void deWeight(Set<Character> setA, Set<Character> setB, Set<Character> setC) {
        if (setA.size() == 1) {
            setB.removeAll(setA);
            setC.removeAll(setA);
        }
        if (setB.size() == 1) {
            setA.removeAll(setB);
            setC.removeAll(setB);
        }
        if (setC.size() == 1) {
            setA.removeAll(setC);
            setB.removeAll(setC);
        }
    }

}

 

标签:hj,setA,setB,setC,System,三三,println,对决,out
来源: https://www.cnblogs.com/hua-jian/p/15484695.html