贪心算法实现班级平均分组

2024-09-03 20:28

本文主要是介绍贪心算法实现班级平均分组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

需求分析

**业务需求 : 二年级一班级有 50人 ,分为10个小组.
分组规则:

1 各个组的男女比例要大致平衡
2 各组成员的身高和要大致相等 例如 1组 身高和 = 2 组身高和 = 3 组. 以此类推
3 各组成员的总分和要大致相等 例如 1组 身高和 = 2 组身高和 = 3 组. 以此类推


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;public class DataGrouper {public static List<List<Person>> groupPeople(List<Person> people, int numGroups, int peoplePerGroup) {List<List<Person>> groups = new ArrayList<>();for (int i = 0; i < numGroups; i++) {groups.add(new ArrayList<>());}// Sort people by gender and then some other criteria if neededCollections.sort(people, Comparator.comparing(Person::getGender));// Simple greedy approachfor (Person person : people) {int bestGroup = -1;int minDiff = Integer.MAX_VALUE;// Find the group with the smallest difference in total heightfor (int i = 0; i < numGroups; i++) {int currentTotalHeight = groups.get(i).stream().mapToInt(Person::getHeight).sum();int diff = Math.abs(targetHeightPerGroup - currentTotalHeight);if (diff < minDiff && groups.get(i).size() < peoplePerGroup) {minDiff = diff;bestGroup = i;}}if (bestGroup != -1) {groups.get(bestGroup).add(person);}}// Note: This implementation does not guarantee perfect balance in height or score,// but tries to keep groups roughly balanced in size and gender.// For exact balancing, a more complex algorithm (e.g., dynamic programming, backtracking) would be needed.return groups;}// This is a placeholder for the target height per group, which should be calculated based on total height and number of groupsprivate static int targetHeightPerGroup = 0; // You would need to calculate this based on total height and number of groupspublic static void main(String[] args) {// Example usageList<Person> people = new ArrayList<>();people.add(new Person("吴思彤", "F", 160, 374.5));people.add(new Person("方新苗", "F", 159, 388.0));people.add(new Person("冯洛伊", "F", 149, 373.0));people.add(new Person("吴雨伦", "M", 164, 368.5));people.add(new Person("李佳如", "F", 169, 368.5));people.add(new Person("李浩泽", "M", 155, 364.0));people.add(new Person("周政宏", "M", 187, 364.0));people.add(new Person("骆思媛", "F", 156, 362.0));people.add(new Person("陶洁", "F", 172, 360.0));people.add(new Person("陈炅", "M", 142, 357.0));people.add(new Person("宋浩文", "M", 150, 356.0));people.add(new Person("郭凯鸣", "M", 164, 353.0));people.add(new Person("江诗莹", "F", 150, 352.5));people.add(new Person("陈子轩", "M", 156, 349.0));people.add(new Person("唐梓航", "M", 159, 347.5));people.add(new Person("董凌希", "F", 149, 346.5));people.add(new Person("黄紫依", "F", 164, 346.5));people.add(new Person("程涵薇", "F", 164, 343.0));people.add(new Person("王国林", "M", 170, 340.5));people.add(new Person("罗晨又", "M", 162, 335.5));people.add(new Person("林依辰", "F", 150, 332.5));people.add(new Person("顾恩赫", "M", 150, 330.0));people.add(new Person("华子雨", "F", 142, 327.5));people.add(new Person("吴若云", "F", 164, 326.5));people.add(new Person("林夏", "F", 149, 326.0));people.add(new Person("袁代隆", "M", 143, 318.5));people.add(new Person("王玉鑫", "M", 155, 318.0));people.add(new Person("郑书扬", "M", 165, 312.5));people.add(new Person("黄帮伟", "M", 142, 312.5));people.add(new Person("薛颂业", "M", 150, 308.5));people.add(new Person("杨凯迪", "M", 151, 306.0));people.add(new Person("杨景皓", "M", 175, 303.5));people.add(new Person("牛恺文", "M", 151, 302.5));people.add(new Person("吴子轩", "M", 156, 298.5));people.add(new Person("吴俊熙", "M", 160, 288.5));people.add(new Person("陈睿", "M", 170, 271.5));people.add(new Person("钱辰佑", "M", 158, 271.5));people.add(new Person("黄蔚哲", "M", 160, 262.5));people.add(new Person("靳梓轩", "F", 168, 260.5));people.add(new Person("唐瑶", "F", 168, 247.5));people.add(new Person("孙亚煊", "F", 164, 244.5));people.add(new Person("陈可馨", "F", 154, 208.0));people.add(new Person("卢婧雯", "F", 154, 203.0));people.add(new Person("宋鑫", "M", 155, 163.5));people.add(new Person("盛诗祺", "F", 150, 161.5));people.add(new Person("杨珂", "F", 162, 91.0));// Add people to the list...// 分8组int numGroups = 8;// 每组6人int peoplePerGroup = 6;// Calculate target height per group based on total height and number of groups (not shown here)List<List<Person>> groups = groupPeople(people, numGroups, peoplePerGroup);int count = 0;// Print or process the groups...for (int i = 0; i < groups.size(); i++) {System.out.print("Group " + (i + 1) + ": ");System.out.print("    身高和" + groups.get(i).stream().mapToInt(Person::getHeight).sum());System.out.print("    总分和" + groups.get(i).stream().mapToDouble(Person::getTotalScore).sum());int M = groups.get(i).stream().filter(stu -> "M".equals(stu.getGender())).collect(Collectors.toList()).size();int F = groups.get(i).stream().filter(stu -> "F".equals(stu.getGender())).collect(Collectors.toList()).size();System.out.println("    男: " + M + "  女: " + F);for (int j = 0; j < groups.get(i).size(); j++) {count = count + 1;System.out.println(groups.get(i).get(j) + "  -  " + j);}System.out.println(count);}}
}class Person {String name;String gender;int height;double totalScore;public Person(String name, String gender, int height, double totalScore) {this.name = name;this.gender = gender;this.height = height;this.totalScore = totalScore;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public double getTotalScore() {return totalScore;}public void setTotalScore(double totalScore) {this.totalScore = totalScore;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", gender='" + gender + '\'' +", height=" + height +", totalScore=" + totalScore +'}';}
}

输出结果, 由于数据量比较小,看起来不够平衡, 数据量越大会越平衡

Group 1:     身高和939    总分和1840.5: 3: 3
Person{name='吴思彤', gender='F', height=160, totalScore=374.5}  -  0
Person{name='林夏', gender='F', height=149, totalScore=326.0}  -  1
Person{name='卢婧雯', gender='F', height=154, totalScore=203.0}  -  2
Person{name='郭凯鸣', gender='M', height=164, totalScore=353.0}  -  3
Person{name='黄帮伟', gender='M', height=142, totalScore=312.5}  -  4
Person{name='陈睿', gender='M', height=170, totalScore=271.5}  -  5
6
Group 2:     身高和832    总分和1727.0: 3: 2
Person{name='方新苗', gender='F', height=159, totalScore=388.0}  -  0
Person{name='吴若云', gender='F', height=164, totalScore=326.5}  -  1
Person{name='吴雨伦', gender='M', height=164, totalScore=368.5}  -  2
Person{name='王国林', gender='M', height=170, totalScore=340.5}  -  3
Person{name='杨景皓', gender='M', height=175, totalScore=303.5}  -  4
11
Group 3:     身高和942    总分和1814.0: 3: 3
Person{name='冯洛伊', gender='F', height=149, totalScore=373.0}  -  0
Person{name='黄紫依', gender='F', height=164, totalScore=346.5}  -  1
Person{name='盛诗祺', gender='F', height=150, totalScore=161.5}  -  2
Person{name='陈子轩', gender='M', height=156, totalScore=349.0}  -  3
Person{name='郑书扬', gender='M', height=165, totalScore=312.5}  -  4
Person{name='钱辰佑', gender='M', height=158, totalScore=271.5}  -  5
17
Group 4:     身高和960    总分和1798.0: 4: 2
Person{name='李佳如', gender='F', height=169, totalScore=368.5}  -  0
Person{name='靳梓轩', gender='F', height=168, totalScore=260.5}  -  1
Person{name='李浩泽', gender='M', height=155, totalScore=364.0}  -  2
Person{name='罗晨又', gender='M', height=162, totalScore=335.5}  -  3
Person{name='杨凯迪', gender='M', height=151, totalScore=306.0}  -  4
Person{name='宋鑫', gender='M', height=155, totalScore=163.5}  -  5
23
Group 5:     身高和927    总分和1896.5: 3: 3
Person{name='骆思媛', gender='F', height=156, totalScore=362.0}  -  0
Person{name='华子雨', gender='F', height=142, totalScore=327.5}  -  1
Person{name='孙亚煊', gender='F', height=164, totalScore=244.5}  -  2
Person{name='宋浩文', gender='M', height=150, totalScore=356.0}  -  3
Person{name='王玉鑫', gender='M', height=155, totalScore=318.0}  -  4
Person{name='吴俊熙', gender='M', height=160, totalScore=288.5}  -  5
29
Group 6:     身高和828    总分和1604.0: 3: 2
Person{name='陶洁', gender='F', height=172, totalScore=360.0}  -  0
Person{name='唐瑶', gender='F', height=168, totalScore=247.5}  -  1
Person{name='周政宏', gender='M', height=187, totalScore=364.0}  -  2
Person{name='顾恩赫', gender='M', height=150, totalScore=330.0}  -  3
Person{name='牛恺文', gender='M', height=151, totalScore=302.5}  -  4
34
Group 7:     身高和895    总分和1867.0: 3: 3
Person{name='江诗莹', gender='F', height=150, totalScore=352.5}  -  0
Person{name='林依辰', gender='F', height=150, totalScore=332.5}  -  1
Person{name='陈可馨', gender='F', height=154, totalScore=208.0}  -  2
Person{name='陈炅', gender='M', height=142, totalScore=357.0}  -  3
Person{name='袁代隆', gender='M', height=143, totalScore=318.5}  -  4
Person{name='吴子轩', gender='M', height=156, totalScore=298.5}  -  5
40
Group 8:     身高和944    总分和1699.0: 3: 3
Person{name='董凌希', gender='F', height=149, totalScore=346.5}  -  0
Person{name='程涵薇', gender='F', height=164, totalScore=343.0}  -  1
Person{name='杨珂', gender='F', height=162, totalScore=91.0}  -  2
Person{name='唐梓航', gender='M', height=159, totalScore=347.5}  -  3
Person{name='薛颂业', gender='M', height=150, totalScore=308.5}  -  4
Person{name='黄蔚哲', gender='M', height=160, totalScore=262.5}  -  5
46

这篇关于贪心算法实现班级平均分组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1133960

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time