zoukankan      html  css  js  c++  java
  • 利用C#队列集合(Queue)写的一个算法

    C#队列集合类在工作当中很少用到,昨天偶然听到同学工作时的一个需求(资源重新平均分配,但自己的资源不能分配给自己),我自己想了想,也查了查,感觉有必要用下队列Queue,这样实现起来比较简单些。

    一下是实现的代码:

    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
    Dictionary<long, List<long>> map = new Dictionary<long, List<long>>();
    List<long> list1 = new List<long>();
    list1.Add(1L);
    list1.Add(2L);
    list1.Add(3L);
    list1.Add(4L);
    list1.Add(5L);
    list1.Add(6L);
    list1.Add(7L);
    list1.Add(8L);
    list1.Add(9L);
    list1.Add(10L);
    list1.Add(11L);
    list1.Add(13L);

    List<long> list2 = new List<long>();

    list2.Add(11L);
    list2.Add(12L);
    list2.Add(13L);
    list2.Add(14L);
    list2.Add(15L);

    List<long> list3 = new List<long>();
    list3.Add(31L);
    list3.Add(32L);
    list3.Add(33L);
    list3.Add(34L);
    list3.Add(35L);
    list3.Add(36L);
    list3.Add(37L);
    list3.Add(38L);
    list3.Add(39L);

    List<long> list4 = new List<long>();

    list4.Add(41L);
    list4.Add(42L);
    list4.Add(43L);
    list4.Add(44L);

    List<long> list5 = new List<long>();

    list4.Add(63L);
    list4.Add(73L);
    list4.Add(83L);
    list4.Add(93L);
    list4.Add(64L);
    list4.Add(74L);
    list4.Add(84L);
    list4.Add(94L);

    List<long> list6 = new List<long>();
    List<long> list7 = new List<long>();
    List<long> list8 = new List<long>();
    List<long> list9 = new List<long>();


    map.Add(1L, list1);
    map.Add(3L, list3);
    map.Add(2L, list2);
    map.Add(4L, list4);
    map.Add(5L, list5);
    map.Add(6L, list6);
    map.Add(7L, list7);
    map.Add(8L, list8);

    Queue<long> queue = new Queue<long>();
    foreach (var item in map)
    {
    foreach (long val in item.Value)
    {
    queue.Enqueue(val);
    }
    }

    //新建所有用户的集合,每个用户放置平分后的资源
    Dictionary<long, List<long>> data = new Dictionary<long, List<long>>();
    int avg = queue.Count / map.Count; //获取平均分配数
    int remain = queue.Count % map.Count; //获取剩余数
    while(queue.Count>0)
    {
    bool isBreak=true;
    foreach (var item in map)
    {
    if (!data.ContainsKey(item.Key))
    {
    data.Add(item.Key, new List<long>());
    }
    if(item.Value.Contains(queue.Peek())||data[item.Key].Count>=avg)
    continue;
    else
    {
    isBreak=false;
    data[item.Key].Add(queue.Dequeue());
    }
    }
    if(isBreak)
    break;
    }
    Random random = new Random();
    for (int i = 0; i < remain; i++)
    {
    int rand = random.Next(data.Count);
    data.ElementAt(rand).Value.Add(queue.Dequeue());
    }
    foreach (var dd in data)
    {
    Console.Write(dd.Key+":");
    foreach (long d in dd.Value)
    {
    Console.Write(d+",");
    }
    Console.WriteLine();
    }

    stopwatch.Stop(); // 停止监视
    Console.WriteLine("所用时间:" + stopwatch.ElapsedMilliseconds);
    Console.Read();

    自己的资源不能分给自己,首先要把所有资源平分,然后平分的时候看是不是自己的。已经分过的就不再分了。剩余的没分完的随机分给所有人。

  • 相关阅读:
    12月12日总结
    练习:请用索引取出下面list的指定元素:
    练习:小明身高1.75,体重80.5kg。请根据BMI公式(体重除以身高的平方)帮小明计算他的BMI指数,并根据BMI指数:
    练习:请利用循环依次对list中的每个名字打印出Hello, xxx!:
    练习:学员管理系统
    练习:请修改列表生成式,通过添加if语句保证列表生成式能正确地执行
    CF1067D Computer Game
    高等数学第三章
    CF755G PolandBall and Many Other Balls
    TS泛型工具
  • 原文地址:https://www.cnblogs.com/cnzryblog/p/5434580.html
Copyright © 2011-2022 走看看