0%

公司有n个组,每组人数相同,>=1人,需要进行随机的组队吃饭


题目一:公司有n个组,每组人数相同,>=1人,需要进行随机的组队吃饭。

要求:

  1. 两两一队,不能落单,落单则三人一队

  2. 一个人只出现一次

  3. 队伍中至少包含两个组

  4. 随机组队,重复执行程序得到的结果不一样

举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GroupList = [  # 小组列表

['小名', '小红', '小马', '小丽', '小强'],

['大壮', '大力', '大1', '大2', '大3'],

['阿花', '阿朵', '阿蓝', '阿紫', '阿红'],

['A', 'B', 'C', 'D', 'E'],

['一', '二', '三', '四', '五'],

['建国', '建军', '建民', '建超', '建跃'],

['爱民', '爱军', '爱国', '爱辉', '爱月']
]

输入:GroupList
输出:(A, 小名),(B, 小红)。。。

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random


def interview(GroupList):
result = [[]]
bit_index = 0
grouplist_y = len(GroupList)
grouplist_x = len(GroupList[0])
last_index = -1

while bit_index < grouplist_y:
rand_y = random.randint(0, grouplist_y-1)
if last_index == rand_y:
continue

rand_x = random.randint(0, grouplist_x-1)
if len(GroupList[rand_y]) == 0 or len(GroupList[rand_y]) <= rand_x:
# print(GroupList)
# print(len(GroupList[rand_y]), rand_x)
continue
val = GroupList[rand_y][rand_x]
GroupList[rand_y] = GroupList[rand_y][:rand_x] + GroupList[rand_y][rand_x + 1:]
result[len(result) - 1].append(val)
last_index = rand_y
if len(result[len(result) - 1]) >= 2:
last_index = -1
result.append([])

if len(GroupList[rand_y]) == 0:
bit_index += 1
if len(result[len(result) - 1]) == 1:
temp = result[len(result)-1]
result = result[:len(result) - 1]
result[len(result) - 1].extend(temp)

if len(result[len(result) - 1]) == 0:
return result[:len(result) - 1]

return result







if __name__ == '__main__':
GroupList = [ # 小组列表
['小名', '小红', '小马', '小丽', '小强'],
['大壮', '大力', '大1', '大2', '大3'],
['阿花', '阿朵', '阿蓝', '阿紫', '阿红'],
['A', 'B', 'C', 'D', 'E'],
['一', '二', '三', '四', '五'],
['建国', '建军', '建民', '建超', '建跃'],
['爱民', '爱军', '爱国', '爱辉', '爱月']
]

result = interview(GroupList)
print(result)


golang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import (
"fmt"
"math/rand"
"time"
)

var GroupList = [][]string{
{"小名", "小红", "小马", "小丽", "小强"},
{"大壮", "大力", "大1", "大2", "大3"},
{"A", "B", "C", "D", "E"},
{"建国", "建军", "建民", "建超", "建跃"},
{"一", "二", "三", "四", "五"},
}

func funcGroupList() [][]string {
result := [][]string{[]string{}}
bit_index := 0
grouplist_y := len(GroupList)
grouplist_x := len(GroupList[0])
last_index := -1
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for bit_index < grouplist_y {
rand_y := r.Intn(grouplist_y)
if last_index == rand_y {
continue
}
rand_x := r.Intn(grouplist_x)
if len(GroupList[rand_y]) == 0 || len(GroupList[rand_y]) <= rand_x {
continue
}
val := GroupList[rand_y][rand_x]
GroupList[rand_y] = append(GroupList[rand_y][:rand_x], GroupList[rand_y][rand_x+1:]...)
result[len(result)-1] = append(result[len(result)-1], val)
last_index = rand_y
if len(result[len(result)-1]) >= 2 {
last_index = -1
result = append(result, []string{})
}
if len(GroupList[rand_y]) == 0 {
bit_index++
}
}
if len(result[len(result)-1]) == 1 {
temp := result[len(result)-1]
result = result[:len(result)-1]
result[len(result)-1] = append(result[len(result)-1], temp...)
}
if len(result[len(result)-1]) == 0 {
return result[:len(result)-1]
}
return result
}

func main() {
result := funcGroupList()
fmt.Println(result)
}

------------- 本文结束 感谢您的阅读-------------