An algorithm written using the C # queue set

Source: Internet
Author: User

C # Queue Collection class is seldom used in the work, yesterday overheard the classmate work a demand (resource redistribution, but their resources can not be assigned to their own), I think about it, also checked, feel the need to use the queue, so the implementation is relatively simple.

Here is the code for implementation:

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);
}
}

//Create a new collection of all users, and each user places a split resource
Dictionary<long, list<long>> data = new Dictionary<long, List<long >> ();
int avg = queue. Count/map. Count; Gets the average number of allocations
int remain = queue. Count% map. Count; Gets the remainder of the
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 (); Stop monitoring
Console.WriteLine ("Time used:" + stopwatch. Elapsedmilliseconds);
Console.read ();

Own resources can not be assigned to themselves, first of all the resources to split, and then split the time to see whether it is their own. The points that have been divided are no longer divided. The remaining random points are assigned to everyone.

An algorithm written using the C # queue set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.