发此问题时本人已经过多般思考和编写不能实现理想结果,不是那种点点问题就来问的人`希望知道的
帮下!
例:有4个任务分配a,b,c三个人a现有11个任务,b现有2个任务,c有3个任务,如何分配最平均但又不能低于
现在有任务,结果最终为a=11,b=4,c=5或a=11,b=5,c=4都行。
有30个任务分配啊a现有2,b现有2,c现有10分配结果最终 a=14 b=15 c=15或a=15 b=15 = c14等。
现在这样是写死了的·要求需分配任务不确定,要分配的人不确定来自于数据库某张表的所有行。
现有任务是表里某行所对应的字段。用JAVA或存储过程能实现就行,可以把代码写死,但改动数据也能成功
写不下的话发我邮箱ras_12312312@163.com,万分感谢!
[color=red]task方法是实现方法,taskList内的值是你数据库里取出的任务记录,你数据库取时加一下排序,list里的值是从小到大。taskCount是待分配任务总数,代码写的不怎么规范,就是为了实现功能而已。
[/color]
[code="java"]
public static List task(List taskList,Integer taskCount){
for(int i=1;i int temp;
temp =(taskList.get(i)-taskList.get(i-1))*i;
if(taskCount/temp != 0){
a(taskList,i,temp);
}else{
a(taskList,i,taskCount);
taskCount = taskCount -temp;
break;
}
taskCount = taskCount -temp;
if(taskCount break;
}
}
if(taskCount>0){
a(taskList,taskList.size(),taskCount);
}
return taskList;
}
public static boolean a(List taskList,int k,int temp){
boolean res = false;
for(int i=0;i taskList.set(i, taskList.get(i)+temp/k);
}
if(temp/k == 0){
for(int j=0;j taskList.set(j, taskList.get(j)+1);
res =true;
}
}
if(temp>k){
for(int j=0;j<temp%k;j++){
taskList.set(j, taskList.get(j)+1);
res =true;
}
}
return res;
}
[/code]
public class Person {
private String name;
private int initTask=0;
private int currTask=0;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getInitTask() {
return initTask;
}
public void setInitTask(int initTask) {
this.initTask = initTask;
}
public int getCurrTask() {
return currTask;
}
public void setCurrTask(int currTask) {
this.currTask = currTask;
}
public Person(String name, int initTask) {
this.name = name;
this.initTask =initTask;
this.currTask=initTask;
}
public void addcurrTaskOne()
{
currTask++;
}
}
public class Problem {
private Vector persons;
private int assignTask;
public Problem(Vector<Person> persons, int assignTask) {
this.persons = persons;
this.assignTask = assignTask;
}
@SuppressWarnings("unused")
private Person getMinCurrTaskObject() {
Person minCurrTask;
Person preP = persons.get(0);
minCurrTask = preP;
for (Person currP : persons) {
if (currP.getCurrTask() < preP.getCurrTask()) {
minCurrTask = currP;
}
}
return minCurrTask;
}
public void addPersonTask() {
while (assignTask > 0) {
getMinCurrTaskObject().addcurrTaskOne();
assignTask--;
}
}
public void displayResult() {
for (Person p : persons) {
System.out.println("name:" + p.getName() + ", initTask:"
+ p.getInitTask() + ", currentTask:" + p.getCurrTask()
+ "; ");
}
}
public static void main(String[] args) {
Vector<Person> persons = new Vector<Person>();
Person p1 = new Person("d", 9);
Person p2 = new Person("c", 7);
Person p3 = new Person("b", 5);
Person p4 = new Person("a", 9);
persons.add(p1);
persons.add(p2);
persons.add(p3);
persons.add(p4);
Problem problem = new Problem(persons, 11);
problem.addPersonTask();
problem.displayResult();
}
}