对于一组数据array,平均分配成m组,要求每组的数量差不多。
思路:先对array数据进行排序,计算出可以分成n组。
然后将array数据分成n组已经排序好的数据集合。
将N组数据分正向赋值到M中,再反向赋值到M中,如此反复到结束。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace fenpei
{
public class Person
{
public Person(string Name, int Capability)
{
this._name = Name;
this._capability = Capability;
}
private string _name;
private int _capability;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Capability
{
get { return _capability; }
set { _capability = value; }
}
}
class Program
{
static void Main(string[] args)
{
Person p1 = new Person("NAME1", 201);
Person p2 = new Person("NAME2", 233);
Person p3 = new Person("NAME3", 189);
Person p4 = new Person("NAME4", 198);
Person p5 = new Person("NAME5", 164);
Person p6 = new Person("NAME6", 181);
Person p7 = new Person("NAME7", 212);
Person p8 = new Person("NAME8", 205);
Person p9 = new Person("NAME9", 192);
Person p10 = new Person("NAME10", 241);
Person p11 = new Person("NAME11", 136);
Person p12 = new Person("NAME12", 201);
Person p13 = new Person("NAME13", 142);
Person p14 = new Person("NAME14", 127);
Person p15 = new Person("NAME15", 189);
Person p16 = new Person("NAME16", 221);
List<Person> personList = new List<Person>();
personList.Add(p1);
personList.Add(p2);
personList.Add(p3);
personList.Add(p4);
personList.Add(p5);
personList.Add(p6);
personList.Add(p7);
personList.Add(p8);
personList.Add(p9);
personList.Add(p10);
personList.Add(p11);
personList.Add(p12);
personList.Add(p13);
personList.Add(p14);
personList.Add(p15);
// personList.Add(p16);
Sort(personList, 3);
Console.Read();
}
/// <summary>
/// 表示分组的作用
/// </summary>
/// <param name="personList">总人数列表</param>
/// <param name="GroupCount">分组数量</param>
static void Sort(List<Person> personList, int GroupCount)
{
if (GroupCount <= personList.Count)
{
//先排序
personList.Sort(Compare);
//可以取的次数
int QU = Convert.ToInt32(personList.Count / GroupCount) + 1;
List<List<Person>> pList = new List<List<Person>>();
//排序后的数量
List<List<Person>> sortList = new List<List<Person>>();
//分组排序后的数据
for (int j = 0; j < QU; j++)
{
List<Person> getPerson = new List<Person>();
for (int i = 0; i < GroupCount; i++)
{
int index = 0;
if (j == 0)
{
index = i + j;
}
else
{
index = (j) * GroupCount + i;
}
if (index < personList.Count)
{
getPerson.Add(personList[index]);
}
}
if (getPerson.Count > 0)
{
sortList.Add(getPerson);
}
}
//开始分配
for (int j = 0; j < GroupCount; j++)
{
List<Person> listPerson = new List<Person>();
bool sort = true;
for (int i = 0; i < sortList.Count; i++)
{
//正向分
if (sort)
{
if (j < sortList[i].Count)
{
listPerson.Add(sortList[i][j]);
}
sort = false;
}
else//反向分
{
if (GroupCount - (j + 1) < sortList[i].Count && GroupCount - (j + 1) >= 0)
{
listPerson.Add(sortList[i][GroupCount - (j + 1)]);
}
sort = true;
}
}
if (listPerson.Count > 0)
{
pList.Add(listPerson);
}
}
int m = 0;
foreach (List<Person> lp in pList)
{
m++;
Console.Write("当前第" + m.ToString() + "组\r\n");
int totalCa = 0;
foreach (Person pp in lp)
{
totalCa = totalCa + pp.Capability;
Console.Write("能力:" + pp.Capability + "名字:" + pp.Name + "\r\n");
}
Console.Write("总能力:" + totalCa.ToString() + "\r\n");
Console.Write("结束\r\n");
}
}
else
{
Console.Write("无法分组,分组数量大于总人数\r\n");
}
}
public static int Compare(Person oPerson, Person nPerson)
{
if (oPerson == null)
{
if (nPerson == null)
{
return 0;
}
else
{
return -1;
}
}
else
{
if (nPerson == null)
{
return 1;
}
else
{
if (oPerson.Capability > nPerson.Capability)
{
return 1;
}
else if (oPerson.Capability == nPerson.Capability)
{
return 0;
}
else
{
return -1;
}
}
}
}
}
}
分享到:
相关推荐
本程序是用c#2008编写的,包括递归的有重复元素的排列问题和半数集问题,动态规划的导弹问题,贪心算法的找零钱问题。
17. 在捕获(catch)语句的抛出异常子句中(throw),总是抛出原始异常维护原始错误的堆栈分配。 catch(Exception exception) { MessageBox.Show(exception.Message); throw ; //和throw exception一样。 } ...
由三个类实现,写在了一个 Java 文件中:...代码中运用了命令模式,如若能配以监听器,用上观察者模式来控制 UI 显示就更绝妙不过了,就能实现像下载中的区块着色跳跃的动感了,在此定义下一步的着眼点了。
基于深度强化学习的车联网通信资源分配优化系统(python源码带注释).zip基于深度强化学习的车联网通信资源分配优化系统(python源码带注释).zip基于深度强化学习的车联网通信资源分配优化系统(python源码带注释).zip...
代码有WPF分页的实现; TabControl的TabItem左侧排列且文字竖排的实现!
TripCalculator 一个简单的RESTful应用程序,可帮助个人知道如何平均解决团体旅行中产生的不相称费用。部署方式使用Visual Studio 2012或更高版本以及构建和部署。用法将包含费用信息的JSON有效负载发布到/ api / ...
当网格是数据绑定时,列关键字是自动分配给字段名称,或者您也可以采用代码来对它们进行赋值。然后,您可以使用ColIndes(ColKey)语法来引用一列,即使用户已经将其移动到网格上不同的位置,也能检索到您需要的列。 ...
本程序主要是BloomFilter算法的简化实现 因为C#非安全代码无法直接分配内存块,使用了int型数组代替,暂时为了简单没有使用位运算,比位运算消耗内存多16倍。 算法原理: 其首先申请一块大内存,并把内存中...
市场分配示例–超级简单股票市场#要求提供有效的源代码,该代码将: 对于给定的库存, 给定任何价格作为输入,计算股息收益率给定任何价格作为输入,计算市盈率记录交易,时间戳,股票数量,买入或卖出指标以及根据...
计算三门课程平均成绩的函数average();分别用来设置和获取学生各个属性值的set()和get()成员函数(例如:设置数学成绩的成员函数setMath().返回数学成绩的成员函数 getMath());计算三门课程总成绩的函数sum();显示...
方法及其说明如下: 平均值(单击以展开) 1.从图像中获取所有颜色2.创建3个独立的变量,分别称为R,G和B 3.分配与变量名称相对应的颜色值的总和(R =所有R值) 4.将每个总和除以总颜色的数量5.用结果创建新颜色...
当网格是数据绑定时,列关键字是自动分配给字段名称,或者您也可以采用代码来对它们进行赋值。然后,您可以使用ColIndes(ColKey)语法来引用一列,即使用户已经将其移动到网格上不同的位置,也能检索到您需要的列。 ...