萌新问下怎么实现
比如我有12个人,分成3组,第1、2、3个为每组的组长,不进行匹配。剩余的人随机分配,每次每组分配到的人,下次不匹配这组。
我感觉自己想的太笨了 ,问问大佬们好的方法,谢谢。
#include <iostream>
#include <vector>
#include <random>
using namespace std;
int main() {
//有12个人
vector<int> persons= {1,2,3,4,5,6,7,8,9,10,11,12};
//分成3组
vector<vector<int> >groups;
//组长
for (int i = 0; i < 3; ++i) {
groups.push_back(vector<int>{persons});
}
//随机分配
default_random_engine e;
uniform_int_distribution<unsigned> u(0, 2); //随机数分布对象
int temp = -1;
int random = -1;
for (int i = 3; i < persons.size(); ++i) {
temp = u(e);
while (temp == random){
temp = u(e);
}
random = temp;
groups.push_back(persons);
}
for (int i = 0; i < groups.size(); i++){
for (int j = 0; j < groups.size(); ++j) {
cout<<groups<<" ";
}
cout<< endl;
}
return 0;
}
取除123以后数据,然后写不重复,然后随机匹配 什么?你太笨了?下载反诈中心APP了吗?没有的话我能骗骗你吗? package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
//有12个人
persons :=[]int{1,2,3,4,5,6,7,8,9,10,11,12}
//分成3组
groups := make([][]int, 3)
//组长
for i := 0; i < 3; i++ {
groups = append(groups, persons)
}
//随机分配
rand.Seed(time.Now().UnixNano())
temp, random := -1, -1
for i := 3; i < len(persons); i++ {
temp = rand.Intn(3)
//防止重复
for temp == random{
temp = rand.Intn(3)
}
random = temp
groups = append(groups, persons)
}
fmt.Printf("%v\n",groups)
}
页:
[1]