public class Fenpei {
static List<Integer> listCus = new ArrayList<Integer>();
static List<String> persons = new ArrayList<String>();
static List<Persons> result = new ArrayList<Persons>();
public static void main(String[] args) {
int k = 0;
List l = new ArrayList();
// 发、数量
for (int i = 0; i < 20; i++) {
listCus.add(i);
}
// 人
for (int j = 0; j < 5; j++) {
persons.add("person" + j);
}
int personSize = persons.size();
for (int i = 0; i < listCus.size(); i++) {
Persons p = new Persons();
p.setI(i);
p.setPersonname(persons.get(i % personSize));
result.add(p);
}
Iterator it = result.iterator();
while (it.hasNext()) {
Persons p = (Persons) it.next();
System.out.println("哪个人 => " + p.getI() + " 分配到 "
+ p.getPersonname());
}
}
public static class Persons {
private Integer i;
private String personname;
public Integer getI() {
return i;
}
public void setI(Integer i) {
this.i = i;
}
public String getPersonname() {
return personname;
}
public void setPersonname(String personname) {
this.personname = personname;
}
}
}
</pre><pre class="csharp" name="code"></pre><pre class="csharp" name="code">public class FenpeiRen {
/**
* @param args
*/
public 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 ArrayList<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,5);
}
/**
* 分组函数
*
* @param personList
* 总人数列表
* @param groupCount
* 分组数量
*/
static void sort(List<Person> personList, int groupCount) {
// 取得总人数
int personSize = personList.size();
// 如果分组数量小于personsize,则分组
if (groupCount <= personSize) {
// 先排序
Collections.sort(personList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.get_capability().compareTo(p2.get_capability());
}
});
// 可以取多少次
int qU = (personSize / groupCount) + 1;
List<List<Person>> pList = new ArrayList<List<Person>>();
// 排序后的数量
List<List<Person>> sortList = new ArrayList<List<Person>>();
// 分组排序后的数据
for (int i = 0; i < qU; i++) {
List<Person> getPerson = new ArrayList<Person>();
for (int j = 0; j < groupCount; j++) {
int index = 0;
if (j == 0) {
index = i + j;
} else {
index = (j) * groupCount + i;
}
if(index<personSize){
getPerson.add(personList.get(index));
}
}
if(getPerson.size()>0){
sortList.add(getPerson);
}
}
//开始分配
for (int j = 0; j < groupCount; j++) {
List<Person> listPerson = new ArrayList<Person>();
boolean sort = true;
for (int i = 0; i < sortList.size(); i++) {
//正向分
if(sort){
if(j<sortList.get(i).size()){
listPerson.add(sortList.get(i).get(j));
}
sort=false;
}
else{//反向分
if(groupCount-(j+1)<sortList.get(i).size()&&groupCount-(j+1)>=0){
listPerson.add(sortList.get(i).get(groupCount-(j+1)));
}
sort=true;
}
}
if(listPerson.size()>0){
pList.add(listPerson);
}
}
int m = 0;
for (List<Person> lp : pList) {
m++;
System.out.print("当前第 "+m+" 组\r\n");
int totalCa = 0;
for(Person p : lp){
totalCa = totalCa + p.get_capability();
System.out.print("能力 "+p.get_capability()+" 名字 "+p.get_name());
}
System.out.print("总能力 "+totalCa+"\r\n");
System.out.print("结束\r\n");
}
}else{
System.out.println("无法分组,分组数量大于总人数\r\n");
}
// Collection.sort();
}
}