吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 697|回复: 4
收起左侧

[求助] 萌新问下怎么实现

[复制链接]
Grieved 发表于 2021-11-26 17:46
25吾爱币
比如我有12个人,分成3组,第1、2、3个为每组的组长,不进行匹配。
剩余的人随机分配,每次每组分配到的人,下次不匹配这组。
我感觉自己想的太笨了 ,问问大佬们好的方法,谢谢。

最佳答案

查看完整内容

[mw_shl_code=cpp,true]#include #include #include using namespace std; int main() { //有12个人 vector persons = {1,2,3,4,5,6,7,8,9,10,11,12}; //分成3组 vectorgroups; //组长 for (int i = 0; i < 3; ++i) { groups.push_back(vector{persons}); } //随机分配 default_random_engine e; uniform_int_distribution u(0, 2); //随机数分布对象 ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

pzx521521 发表于 2021-11-26 17:46
[C++] 纯文本查看 复制代码
#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[i]});
    }
    //随机分配
    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[random].push_back(persons[i]);
    }
    for (int i = 0; i < groups.size(); i++){
        for (int j = 0; j < groups[i].size(); ++j) {
            cout<<groups[i][j]<<" ";
        }
        cout<< endl;
    }
    return 0;
}
小平平qq 发表于 2021-11-27 09:00
取除123以后数据,然后写不重复,然后随机匹配
夜泉 发表于 2021-11-27 21:18
什么?你太笨了?下载反诈中心APP了吗?没有的话我能骗骗你吗?
pzx521521 发表于 2021-11-29 14:54
[Golang] 纯文本查看 复制代码
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[i] = append(groups[i], persons[i])
	}
	//随机分配
	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[random] = append(groups[random], persons[i])
	}
	fmt.Printf("%v\n",groups)

}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-13 00:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表