两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单,用java实现

这个题目思路有两种,一种挨个abc循环,挨个判断,比较好理解,另一种是取反思想

下面是最终的实现代码:

import java.util.ArrayList;
import java.util.List;

/**
 * 两个乒乓球队进行比赛,各出三人。
 * 甲队为a,b,c三人,乙队为x,y,z三人。
 * 已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
 */
public class PingPongMatch {
    public static void main(String[] args) {
        String[] teamA = {"a", "b", "c"};
        String[] teamB = {"x", "y", "z"};
        // 列出所有可能的比赛名单
        List<String[]> matchList = generateMatchList(teamA, teamB);
        // 根据已知条件筛选出符合要求的名单
        List<String[]> validMatchList = filterMatchList1(matchList);
        // 输出符合要求的比赛名单
        for (String[] match : validMatchList) {
            System.out.println("甲队:" + match[0] + " vs 乙队:" + match[1]);
        }
    }

    // 生成所有可能的比赛名单
    private static List<String[]> generateMatchList(String[] teamA, String[] teamB) {
        List<String[]> matchList = new ArrayList<>();
        for (String a : teamA) {
            for (String b : teamB) {
                matchList.add(new String[]{a, b});
            }
        }
        return matchList;
    }


    private static List<String[]> filterMatchList1(List<String[]> matchList) {
        List<String[]> validMatchList = new ArrayList<>();
        for (String[] match : matchList) {
            String a = match[0];
            String b = match[1];
            if ("a".equals(a) && !"x".equals(b)) {//a不和x比
                validMatchList.add(new String[]{a, b});
            } else if ("b".equals(a)) {
                validMatchList.add(new String[]{a, b});
            } else if ("c".equals(a) && !("x").equals(b) && !("z").equals(b)) {
                validMatchList.add(new String[]{a, b});
            }
        }
        return validMatchList;
    }

    // 根据已知条件筛选出符合要求的名单
    private static List<String[]> filterMatchList(List<String[]> matchList) {
        List<String[]> validMatchList = new ArrayList<>();
        for (String[] match : matchList) {
            String a = match[0];
            String b = match[1];
            if ((!a.equals("a") || !b.equals("x")) && (!a.equals("c") || !b.equals("x")) && (!a.equals("c") || !b.equals("z"))) {  // a不和x比
                validMatchList.add(match);
            }
        }
        return validMatchList;
    }
}

我们先看普通的实现:这个想对比较好理解,挨个循环abc,把符合条件的加到结果集中去

    private static List<String[]> filterMatchList1(List<String[]> matchList) {
        List<String[]> validMatchList = new ArrayList<>();
        for (String[] match : matchList) {
            String a = match[0];
            String b = match[1];
            if ("a".equals(a) && !"x".equals(b)) {//a不和x比
                validMatchList.add(new String[]{a, b});
            } else if ("b".equals(a)) {
                validMatchList.add(new String[]{a, b});
            } else if ("c".equals(a) && !("x").equals(b) && !("z").equals(b)) {
                validMatchList.add(new String[]{a, b});
            }
        }
        return validMatchList;
    }

另外一种就是取反思想:a说他不和x比,正面是第一个人等于a且第二个人等于x,这种条件是不满足的,那么取反就是只要第一个人不等于a或者第二个人不等于x即可

c说他不和x,z比,正面理解就是第一个人等于c且第二个人等于x或者第一个人等于c且第二个人等于z这两种情况不满足条件,这样实际上就和上面a 的那种情形是一样的,只是拆开了两组,所以最终的结论就是:a和c分别满足自己的条件的情况下取交集即可,也就是下面这样的:

第一个人不等于a或者第二个人不等与x

与上

(第一个人不等于c或者第二个人不等与x

或者

第一个人不等于c或者第二个人不等与z)

可以简化为:

第一个人不等于a或者第二个人不等与x

与上

第一个人不等于c或者第二个人不等与x

与上

第一个人不等于c或者第二个人不等与z

代码实现起来就是:

    private static List<String[]> filterMatchList(List<String[]> matchList) {
        List<String[]> validMatchList = new ArrayList<>();
        for (String[] match : matchList) {
            String a = match[0];
            String b = match[1];
            if ((!a.equals("a") || !b.equals("x")) && (!a.equals("c") || !b.equals("x")) && (!a.equals("c") || !b.equals("z"))) {  // a不和x比
                validMatchList.add(match);
            }
        }
        return validMatchList;
    }

ps:a不满足条件取反:

* 甲队:a vs 乙队:y
* 甲队:a vs 乙队:z
* 甲队:b vs 乙队:x
* 甲队:b vs 乙队:y
* 甲队:b vs 乙队:z
* 甲队:c vs 乙队:x
* 甲队:c vs 乙队:y
* 甲队:c vs 乙队:z

c不满足条件取反:

* 甲队:a vs 乙队:x
* 甲队:a vs 乙队:y
* 甲队:a vs 乙队:z
* 甲队:b vs 乙队:x
* 甲队:b vs 乙队:y
* 甲队:b vs 乙队:z
* 甲队:c vs 乙队:y

两种情况取交集就是最终的结果:

* 甲队:a vs 乙队:y
* 甲队:a vs 乙队:z
* 甲队:b vs 乙队:x
* 甲队:b vs 乙队:y
* 甲队:b vs 乙队:z
* 甲队:c vs 乙队:y

猜你喜欢

转载自blog.csdn.net/qq_17805707/article/details/133138960