0

我有一个学校任务,我需要制作一个程序,自动让情侣在学校上课。在这种情况下,有 6 节课,每节课您都与不同的人合作。因此,在第 1 周 persona与person 合作,b接下来的几周他不能与 person 合作a

我编写了一些代码,将班级分成两部分,但我不知道如何每周改变这对夫妇。

这是我已经拥有的代码(抱歉不是英文):

public void maakKoppels() {
    if (leerlingenLijst.size() % 2 == 1) {
        // if you have an odd number of students it adds the "Bye" student
        leerlingenLijst.add(new Leerling("Bye"));
    }

    for (int i = 1; i <= 6; i++) {
        //its needed for 6 lessons so it does it 6 times
        maakKoppels(i, leerlingenLijst);
    }
}

public void maakKoppels(int weekNum, ArrayList<Leerling> leerlingenLijst) {
    int midden = leerlingenLijst.size() / 2; //split the arraylist in 2

    ArrayList lijst1 = new ArrayList();
    for (int j = 0; j < midden; j++) {
        lijst1.add(leerlingenLijst.get(j));
    }

    ArrayList lijst2 = new ArrayList();
    for (int j = leerlingenLijst.size() - 1; j >= midden; j--) {
        lijst2.add(leerlingenLijst.get(j));
    }
    // here it fills the lessons with the 2 lists. weekNum is the lesson
    // number and the name on lijst1 at index 0 couples with the name on
    // lijst2 at index zero
    practica.add(new Practicum(weekNum, lijst1, lijst2));
}
4

3 回答 3

0

最简单的方法...从第一个 arrayList 中的第一个元素开始,并打乱第二个 arrayList 并选择第一个元素.. arylist1 中的第二个->arraylist2 中的第二个.. 依此类推.. 现在将此数据作为键值对添加到 a map .. 下次创建情侣时,再次洗牌第二个数组列表。现在对于每对制作的情侣,首先检查第二个数组列表中的人是否存在于 map 中的相同键。如果不是,则将该值添加到列表中(地图的值),否则再次打乱列表并尝试..您将需要一个键,地图中的列表...

Map<String,List> hm = new HashMap<String, List>();
List<String> l = new ArrayList<String>();
Collections.shuffle(arrayList2);

现在轮到你来实现逻辑了..对不起,我不能给你代码......

于 2014-01-21T14:46:21.713 回答
0

将班级一分为二是一个好的开始。现在您可以在保持 list2 原样的同时旋转list1 并配对 {list1.get(1),list2.get(1)}、{list1.get(2),list2.get(2)} 等。将 list1 旋转一(周)并重新做一遍。

如果您有超过 12 名学生将在您的六周内工作。使用这种方法,在同一个列表中的学生永远不会成为一对。

于 2014-01-21T15:00:14.200 回答
0

一种解决方法如下...

  1. 将整个类拆分为两个数组列表。L1 & L2
  2. 创建一个变量 offset = 0
  3. 第一周将 L1[0] 和 L2[0+offset] 分配为一对,L1[1] 和 L2[1+offset] 作为另一对,依此类推。
  4. 第二周设置偏移量 = 1
  5. 将 L1[0] 和 L2[0+offset] 分配为一对,但这一次,由于偏移值增加,您实际上将 L1[0] 和 L2[1] 分配为一对。6.为了确保你不超过L2的长度,使用(0+offset)%L2.length()

这将确保您每周都有不同的情侣。

于 2014-01-21T15:00:21.797 回答