首页 > 学院 > 开发设计 > 正文

平均分配

2019-11-14 16:32:19
字体:
来源:转载
供稿:网友
        public void DisTaskTest()        {            List<DisTask> task = new List<DisTask>()            {                new DisTask{Name="其它3"},                new DisTask{Name="其它1"},                new DisTask{Name="其它2"},                new DisTask{Name="其它4"},                new DisTask{Name="其它5"},                new DisTask{Name="其它6"},                new DisTask{Name="其它7"},                new DisTask{Name="其它8"},                new DisTask{Name="其它9"},                new DisTask{Name="其它10"},                new DisTask{Name="其它11"}            };            int[] ids = {1,2,3};                      int eachTotal = task.Count/ids.Length;            int remainder = task.Count%ids.Length;            int current = 0;            for (int i = 0; i < ids.Length; i++)            {                for (int j = 0; j < eachTotal; j++)                {                    if (current == eachTotal*(i + 1))                    {                        break;                    }                    task[current].Id = ids[i];                    current++;                }            }                        Random rand = new Random();            for (int i = 0; i < remainder; i++)            {                  int d = rand.Next(0, ids.Length);                task[eachTotal * ids.Length + i].Id = ids[d];            }            Console.WriteLine(remainder);            foreach (var i in task)            {                Console.WriteLine(i.Id+","+i.Name);            }        }        public class DisTask        {            public int Id { get; set; }            public string Name { get; set; }        }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表