Problem: (post path: http://topic.csdn.net/u/20091027/09/db4cd117-bfbe-491f-bcb8-d54ee08c0787.html)
Find an efficient algorithm:
There are 100 children,
There are now a bunch of gifts
If the number of gifts is less than 100, for example 81, the 81 gifts are randomly allocated to children, but make sure that only one person can receive one gift;
If the number of gifts is greater than 100,
For example, if the value is 115, each person has at least one gift. Some people have two gifts, but no one gets more than two;
For example, if the value is 245, each person has at least two gifts. Some people have three gifts, but no one gets more than three;
This is the idea. It is not a question assigned by a university teacher. It is an abstract question in my work,
I hope you can get everyone's ideas, participants, and high scores. Thank you!
It is also an opportunity for communication.
This post makes me feel deeply impressed. We are really excited in the csdn Forum,
This series I will sort out the main solutions
Address: http://blog.csdn.net/yinyuan1987/archive/2009/10/30/4748636.aspx
Series 2: share different solutions or instances
Address: http://blog.csdn.net/yinyuan1987/archive/2009/10/30/4748671.aspx
Series 3: Share your ideas on Performance
Address: http://blog.csdn.net/yinyuan1987/archive/2009/10/30/4748715.aspx
Iii. performance considerations:
Friends on floor 79Asxinyu_usstSaid:
Is it because the number of gifts and the number of people are huge, and the random numbers generated may conflict with each other, resulting in lower efficiency?
I think the above methods are feasible. The key is to use random numbers and disrupt the efficiency of Children's algorithms?
If the number of students is small, it will disrupt the children, which is equivalent to generating a random and full arrangement. Each time the children are arranged in a random and distributed from start to end; I have studied this random and fully-arranged production algorithm and I have a good method to implement it.
If the number of people is large, it is not feasible to generate a random full arrangement. However, we can block the problem and randomly allocate it within the block, I think you should have some requirements on the randomness of the problem abstracted out by random allocation. Otherwise, the problem should be split from start to end.
I think the difference in randomness will affect the efficiency of the algorithm.
A friend quoted gukuitian on the 71 th floor as follows:
I prefer to use list to remove the involved elements. <Br/> set number of people a, number of gifts B, and A> B (A <B Needless to say) </P> <p> I Don't Know How To disrupt arrays. <Br/> 1: from 1 to A, a random number is generated each time, and each element is exchanged once. <br/> 2: two random numbers each time. Exchange two elements, loop N times, (at least a/2 times) </P> <p> extreme point: a = 10000, B = 1; <br/> if you use the list function to generate a random number, <br/> the number of times the array is disrupted is 10000 or 5000. </P> <p> general point: <br/> A list can be used to operate a/2 secondary random number at most. <br/> it takes 10000 or 5000 times to disrupt the array. </P> <p> who can provide a better shuffling method?
This shuffling algorithm is similar to generating a group of fully-arranged data... I have studied... If you need high randomness, you can spend more time ....
Friends on 98 floorSbwwkmydProvided:
Bitmap method to generate random numbers
Public static int [] subgifts100 (INT count) <br/>{< br/> int [] value = new int [100]; <br/> If (count> 0) <br/>{ <br/> int avgvalueless = count/100, avgvalue = avgvalueless + 1, bits; <br/> ulong highbits = 0, lowbits = 0, andbits; <br/> bool issub = (count % = 100) <50; <br/> If (issub) Count = 100-count; <br/> for (random = new random (); count! = 100; count ++) <br/>{< br/> If (BITS = random. next (count)> = 64) highbits = (highbits> (bits-= 64) <(BITS + 1) | (andbits = 1ul <bits) | (highbits & (andbits-1); <br/> else <br/>{< br/> highbits = (highbits <1) | (lowbits> 63); <br/> lowbits = (BITS = 63? 0: (lowbits> bits) <(BITS + 1) | (andbits = 1ul <bits) | (lowbits & (andbits-1 )); <br/>}< br/> for (COUNT = 0; count <64; lowbits> = 1, Count ++) value [count] = issub ^ (lowbits & 1) = 0 )? Avgvalue: avgvalueless; <br/> for (; count <100; highbits >>= 1, Count ++) value [count] = issub ^ (highbits & 1) = 0 )? Avgvalue: avgvalueless; <br/>}< br/> return value; <br/>}< br/> Public static int [] subgifts (INT childrencount, int count) <br/>{< br/> int [] value = NULL; <br/> If (childrencount> 0 & COUNT> = 0) <br/>{< br/> value = new int [childrencount]; <br/> If (count> 0) <br/>{< br/> ulong bitvalue, andbits; <br/> int nextbit, bits, avgvalueless = count/childrencount, avgvalue = avgvalueless + 1, bitsum = 0, Endcount = childrencount & 0x7fffffc0, T = 0; <br/> bool issub = (count % = childrencount) <(childrencount> 1 ); <br/> random = new random (); <br/> If (issub) Count = childrencount-count; <br/> long allcount = (long) count; <br/> for (COUNT = 64; count <= endcount; count + = 64) <br/> {<br/> nextbit = (INT) (allcount * count) /childrencount)-bitsum; <br/> bitsum + = nextbit; <br/> for (bitvalue = 0; nextbit! = 64; nextbit ++) bitvalue = (BITS = random. Next (nextbit) = 63? 0: (bitvalue> bits) <(BITS + 1) | (andbits = 1ul <bits) | (bitvalue & (andbits-1 )); <br/> for (nextbit = count-64; bitvalue! = 0; nextbit ++, bitvalue> = 1) T + = (value [nextbit] = issub ^ (bitvalue & 1) = 0 )? Avgvalue: avgvalueless); <br/> while (nextbit! = Count) T + = (value [nextbit ++] = issub? Avgvalueless: avgvalue); <br/>}< br/> If (endcount = childrencount & 63 )! = 0) <br/>{< br/> nextbit = (INT) allcount-bitsum; <br/> for (bitvalue = 0; nextbit! = Endcount; nextbit ++) bitvalue = (BITS = random. Next (nextbit) = 63? 0: (bitvalue> bits) <(BITS + 1) | (andbits = 1ul <bits) | (bitvalue & (andbits-1 )); <br/> for (nextbit = count-64; bitvalue! = 0; nextbit ++, bitvalue> = 1) T + = (value [nextbit] = issub ^ (bitvalue & 1) = 0 )? Avgvalue: avgvalueless); <br/> while (nextbit! = Childrencount) T + = (value [nextbit ++] = issub? Avgvalueless: avgvalue); <br/>}< br/> return value; <br/>
Wo554006164 test on the 184 floor:
/// <Summary> <br/> // allocate a gift <br/> /// </Summary> <br/> /// <Param name = "p_presentnumber"> GIFT quantity </param> <br/> private void distribution (INT p_presentnumber) <br/>{< br/> const int children_number = 10000; <br/> int [] children = new int [children_number]; // 100 children <br/> int [] shuffle = new int [children_number]; // random number, present by number <br/> hashtable ht = new hashtable (); <br/> int basepresentnum = p_presentnu Mber/children_number; // the minimum number of gifts that children can receive <br/> int residualnum = p_presentnumber % children_number; // The maximum number of gifts <br/> int I = 0; <br/> int rdnum = 1; </P> <p> // generate a random number <br/> while (I <children_number) <br/>{< br/> random = new random (rdnum ++ * datetime. now. millisecond); <br/> int rdresult = random. next (children_number); <br/> If (! Ht. containskey (rdresult) <br/>{< br/> ht. add (rdresult, rdresult); <br/> shuffle [I ++] = rdresult; <br/> // console. writeline ("no." + I + ":" + rdresult. tostring (); <br/>}</P> <p> // send a gift to the child by ID, the first residualnum child will have a gift <br/> for (I = 0; I <children_number; I ++) <br/>{< br/> If (residualnum> I) <br/> Children [shuffle [I] = basepresentnum + 1; <br/> else <br/> Children [shuffle [I] = basepresentnum; <br/>}</P> <p>/* console. writeline ("number of gifts for children:"); <br/> for (I = 0; I <children_number; I ++) <br/> {<br/> console. writeline ("Number" + (I + 1) + ":" + children [I]. tostring () + "gift"); <br/>}< br/> console. writeline ("sort by the number of gifts given to children:"); <br/> for (I = 0; I <children_number; I ++) <br/> {<br/> for (Int J = CHILDREN_NUMBER-1; j> I; j --) <br/>{< br/> If (Children [J]> Children [I]) <br/>{< br/> int temp = children [J]; <br/> Children [J] = children [I]; <br/> Children [I] = temp; <br/>}< br/> for (I = 0; I <children_number; I ++) <br/> {<br/> console. writeline (Children [I]. tostring (); <br/>}*/<br/> // console. read (); <br/>}</P> <p>
I wrote a gift for 0.1 billion, and it took about 1 second for a kid 10 thousand. That's not good!