C#平均分配代码

对于一组数据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;
}
}
}

}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值