[Find an efficient algorithm. If you are interested, try it. High score] solution series 2

Source: Internet
Author: User

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

 

 

 

 

58 floorsSxx_ttA friend uses a database: Amazing!

How to Implement the database with CIDR blocks

-- GN: number of gifts <br/> -- PN: Number of people <br/> -- v_str: the number of people obtained each time during a loop <br/> -- result: the serial number of the selected person at the end of the loop. Each serial number is separated by a comma. </P> <p> If Mod (GN, PN) <> 0 then <br/> dbms_output.put_line ('--- the number of gifts is not a multiple of the number of people -----'); <br/> If floor (GN/PN) <1 then -- when the number of gifts is less than the number of people <br/> v_ SQL: = 'select A from <br/> (select a <br/> from (select Level A from dual <br/> connect by level <= '| PN | ') <br/> order by dbms_random.value <br/>) Where rownum <= '| GN; <br/> elsif Floor (GN/PN)> = 1 then -- when the number of gifts exceeds the number of people <br/> v_ SQL: = 'select A from <br/> (select a <br/> from (select Level A from dual <br/> connect by level <= '| PN | ') <br/> order by dbms_random.value <br/>) Where rownum <= '| Mod (GN, PN); <br/> end if; <br/> open v_cur for v_ SQL; <br/> loop <br/> fetch v_cur into v_str; <br/> exit when v_cur % notfound; <br/> result: = v_str | ',' | result; <br/> end loop; <br/> clo Se v_cur; <br/> If floor (GN/PN) <1 then <br/> dbms_output.put_line ('--- the gainers are' | result ); <br/> elsif floor (GN/PN)> = 1 then <br/> dbms_output.put_line (result | 'can gain' | Ceil (GN/PN) | 'gift, others can gain' | floor (GN/PN) | 'gift !!! '); <Br/> end if; <br/> elsif Mod (GN, PN) = 0 then -- when the number of gifts is an integer multiple of the number of people <br/> dbms_output.put_line ('---- everyone can receive' | GN/PN | 'gift !! '); <Br/> end if; <br/>

 

Friends on the 78 th FloorFeihua317UseJavaA demo of writing the test code, data size, and database execution time:

The following is not the best solution and the cost time is 15-31 Ms.

Package Org. guangguang; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. list; <br/> Import Java. util. map; </P> <p> public class play {</P> <p> Public static void main (string [] ARGs) {<br/> long numberofgifts = 155; <br/> int students = 100; </P> <p> long starttime = system. currenttimemillis (); </P> <p> Map <integer, long> Results = new hashmap <integer, long> (students ); <br/> List <integer> luckystudents = new arraylist <integer> (students); </P> <p> for (INT I = 1; I <= 100; I ++) {<br/> // first every student gets gift in average. <br/> results. put (I, numberofgifts/students); <br/> luckystudents. add (I); <br/>}</P> <p> for (INT restgifts = (INT) (numberofgifts % students); restgifts> 0; restgifts --) {<br/> int luckyno = (INT) (luckystudents. size () * Math. random (); <br/> // increase the gain of the lucky student <br/> integer luckystudent = luckystudents. get (luckyno); <br/> results. put (luckystudent, results. get (luckystudent) + 1); <br/> // remove him from the lucky list, and continue to find lucky student. <br/> luckystudents. remove (luckystudent); <br/>}</P> <p> for (INT I = 1; I <= 100; I ++) {<br/> // print the results <br/> system. out. println ("the student" + I + "get gifts:" + results. get (I); <br/>}</P> <p> system. out. println ("cost time (MS):" + (system. currenttimemillis ()-starttime); <br/>}</P> <p>

If the number of children is only 100, the Code has little impact on the performance. The number of gifts and the number of children are both large, so that you can see the code performance. I changed the number of gifts to more than 0.1 billion, and the number of children was 10 thousand, which took more than 300 milliseconds.Public static void main (string [] ARGs) {<br/> long numberofgifts = 123456789; <br/> int students = 10000; </P> <p> long starttime = system. currenttimemillis (); </P> <p> Map <integer, long> Results = new hashmap <integer, long> (students ); <br/> List <integer> luckystudents = new arraylist <integer> (students); <br/> long average = numberofgifts/students; </P> <p> for (INT I = 1; I <= students; I ++) {<br/> // first every student gets gift in average. <br/> results. put (I, average); <br/> luckystudents. add (I); <br/>}</P> <p> for (INT restgifts = (INT) (numberofgifts % students); restgifts> 0; restgifts --) {<br/> int luckyno = (INT) (luckystudents. size () * Math. random (); <br/> // increase the gain of the lucky student <br/> integer luckystudent = luckystudents. get (luckyno); <br/> results. put (luckystudent, results. get (luckystudent) + 1); <br/> // remove him from the lucky list, and continue to find lucky student. <br/> luckystudents. remove (luckystudent); <br/>}</P> <p> for (INT I = 1; I <= 100; I ++) {<br/> // print the results <br/> system. out. println ("the student" + I + "get gifts:" + results. get (I); <br/>}</P> <p> system. out. println ("cost time (MS):" + (system. currenttimemillis ()-starttime); <br/>}< br/>

 

Friends on 92 floorFall513UseImplemented by Delphi:

Too many upstairs
I'm too lazy to read it.
Let me talk about my ideas. (first, I declare that I will not use Java and only use Delphi to implement it)
AA: array [1 .. 99] of integer; <br/> I: integer; <br/> J: integer; <br/> swap: integer; <br/> for I: = 1 to 99 DO <br/> AA [I]: = I; <br/> for I: = 1 to 99 DO <br/> begin <br/> J: = 1 + random (99); // in Delphi, random is less than 99 from 0, so 1 is required. <br/> swap: = AA [I]; <br/> AA [I]: = J; <br/> AA [J]: = swap; <br/> end; <br/>

 

Then you can check the number you need to take the first few items.
To ensure that the random order is different, you can initialize the random factor.
The code is simple, but it may be hard to understand

 

 

Of course, there are examples of C/C ++ and C # solutions. You can refer to the post and hope to help you.

 

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.