例如,我有一个包含一些讲座实例的列表,每个讲座有一定数量的学生参加本次讲座,还有一个包含一些课堂实例的列表,每个教室都有最大的容量。

现在,我打算为演讲列表中的每个演讲分配一个教室列表中的教室,演讲课中的所有演讲都应该有一个教室,然后创建一个地图来存储这种可能性。
我想以集合的形式返回所有这些可能的匹配。
例如:

Classroom List: [Classroom1(50),Classroom2(70),Classroom3(80)]
Lecture list:   [Lecture1(50), Lecture2(70), Lecture3(50)]

然后,我们有3种可能的地图,分别是:
{lecture1:classroom1, lecture2:classroom2, lecture3:classroom3} and
{lecture1:classroom1, lecture2:classroom3, lecture3:classroom2} and
{lecture1:classroom2, lecture2:classroom3, lecture3:classroom1}

之后,应将所有可能的地图存储在一个集中。

我是编程新手,还没有学习算法,也许这就是为什么我在这个问题上如此努力,如果有人可以帮助我解决这个问题,我将不胜感激。

最佳答案

所以我深陷于此,并写了一个可行的解决方案

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

class ClassMatcher {

        //The set of all possible matchings.
    static ArrayList<ArrayList<Pair>> set = new ArrayList<ArrayList<Pair>>();
        // The current matching being built
    static ArrayList<Pair> cur = new ArrayList<Pair>();

    public static void main(String[] args) {

        Lecture[] l = { new Lecture(50, 1), new Lecture(70, 2), new Lecture(50, 3)};
        ArrayList<Classroom> c = new ArrayList<>(Arrays.asList(
            new Classroom(50, 1), new Classroom(70, 2),
            new Classroom(100, 3)));

        for (int i = 0; i < l.length; i++) {
                    //Fill with dummy values
            cur.add(new Pair(new Classroom(-1, -1), new Lecture(-1, -1)));
        }

        // Sort the arrays to save work in rec()
        Arrays.sort(l);
                //Sort classrooms in descending order
        Collections.sort(c, new Comparator<Classroom>() {
            @Override
            public int compare(Classroom o1, Classroom o2) {
                return o1.compareTo(o2) * -1;
            }
        });

        recursive(l, c, 0);

        // Print all the sets
        for (int i = 0; i < set.size(); i++) {
            System.out.print("{");
            for (int j = 0; j < set.get(i).size(); j++) {
                System.out.print("Lecture " + set.get(i).get(j).l + ": "
                    + "Classroom " + set.get(i).get(j).c);
                if (j < set.get(i).size() - 1) {
                    System.out.print(", ");
                } else {
                    System.out.print("}");
                }
            }
            System.out.println();
        }

    }

    public static void recursive(Lecture[] lectureList,
            ArrayList<Classroom> classroomList, int curLecture) {

        for (int i = 0; i < classroomList.size(); i++) {
            // if the classroom is smaller than the lecture we cna stop as the
            // lists are sorted so all other lectures will be to big for the
            // current classroom
            if (lectureList[curLecture].size > classroomList.get(i).size) {
                return;
            }

            //Match the current classroom to the current lecture and add to the working matching
            cur.set(curLecture, new Pair(classroomList.get(i), lectureList[curLecture]));

                //If there are more lectures to do then remove the used classroom and recursively call.
            if (curLecture < lectureList.length - 1) {
                Classroom tmp = classroomList.remove(i);
                recursive(lectureList, classroomList, curLecture + 1);
                classroomList.add(i, tmp);
            }
                // If no Lectures left then add this matching to the set of all matchings.
            else {
                ArrayList<Pair> copy = (ArrayList<Pair>) cur.clone();
                set.add(copy);
            }
        }

    }

}

class Classroom implements Comparable<Classroom> {

    int size;
    int number;

    public Classroom(int s, int n) {
        size = s;
        number = n;
    }

    @Override
    public int compareTo(Classroom o) {

        return Integer.compare(this.size, o.size);
    }

    public String toString() {
        return number + " (" + size + ")";
    }
}

class Lecture implements Comparable<Lecture> {

    int size;
    int number;

    public Lecture(int s, int n) {
        size = s;
        number = n;
    }

    @Override
    public int compareTo(Lecture o) {

        return Integer.compare(this.size, o.size);
    }

    public String toString() {
        return number + " (" + size + ")";
    }
}

class Pair {

    Classroom c;
    Lecture l;

    public Pair(Classroom c, Lecture l) {
        this.c = c;
        this.l = l;
    }
}

这给出了输出
{Lecture 1 (50): Classroom 3 (100), Lecture 3 (50): Classroom 1 (50), Lecture 2 (70): Classroom 2 (70)}
{Lecture 1 (50): Classroom 2 (70), Lecture 3 (50): Classroom 1 (50), Lecture 2 (70): Classroom 3 (100)}
{Lecture 1 (50): Classroom 1 (50), Lecture 3 (50): Classroom 3 (100), Lecture 2 (70): Classroom 2 (70)}
{Lecture 1 (50): Classroom 1 (50), Lecture 3 (50): Classroom 2 (70), Lecture 2 (70): Classroom 3 (100)}

10-07 13:18
查看更多