Interview Questions: generate an array with a length of 100, and randomly fill each item in the array with numbers ranging from 1 to and ensure that they are not repeated.

Source: Internet
Author: User

This is an interview question:

Refer to the three methods on the Internet for C # implementation:

Method 1:

# Region method 1 <br/> // number of 1-100 records stored <br/> int [] num = new int []; <br/> // indexnum [0] = 1 ;... indexnum [99] = 100; (not repeated) <br/> for (INT I = 0; I <num. length; I ++) <br/>{< br/> num [I] = I + 1; <br/>}< br/> random r = new random (); <br/> // store the final result <br/> int [] result = new int [100]; <br/> int max = 100; // set the maximum random number <br/> for (Int J = 0; j <result. length; j ++) <br/>{< br/> // calculate the randomly generated array index (0-99). Note: upper bound value: 100 cannot be obtained <br/> int Index = R. next (0, max); <br/> // retrieve the number of records stored in the index location <br/> result [J] = num [Index]; <br/> // replace the number that has been put into the result with the last number <br/> // so that the num array from 0 to the max-1 is not saved into the result and not number of duplicates <br/> num [Index] = num [Max-1]; <br/> // the maximum value of the randomly generated array index minus one <br/> MAX --; <br/>}< br/> // print the result <br/> for (INT I = 0; I <100; I ++) <br/>{< br/> if (I % 10 = 0) <br/>{< br/> console. writeline ("/R/N"); <br/>}< br/> console. write (result [I] + ""); <br/>}< br/> console. read (); <br/> # endregion

 

 

 

Method 2:

Dictionary <int, int> result = new dictionary <int, int> (); <br/> random r = new random (INT) datetime. now. ticks); <br/> int temp; <br/> for (INT I = 0; I <100; I ++) <br/> {<br/> DO <br/> {<br/> temp = R. next (1,101); <br/>}< br/> while (result. containsvalue (temp); </P> <p> result. add (I, temp); <br/>}</P> <p> foreach (VAR item in result) <br/>{< br/> If (item. key % 10 = 0) <br/>{< br/> console. writeline ("/R/N"); <br/>}< br/> console. write (item. value + ""); <br/>}< br/> console. read (); <br/> # endregion

 

 

Method 3:

Int [] num = new int [100]; <br/> int temp; <br/> random r = new random (); <br/> for (INT I = 0; I <100; I ++) <br/>{< br/> temp = R. next (1,101); <br/> num [I] = getnumber (Num, I, 1,101, temp, R ); <br/>}</P> <p> for (INT I = 0; I <num. length; I ++) <br/>{< br/> if (I % 10 = 0) <br/>{< br/> console. write ("/R/N"); <br/>}< br/> console. write (Num [I] + ""); <br/>}< br/> console. read ();

 

Getnumber:

/// <Summary> <br/> // recursive comparison of duplicate values <br/> /// </Summary> <br/> static int getnumber (INT [], int index, int minvalue, int maxvalue, int temp, random R) <br/>{< br/> for (INT I = 0; I <index; I ++) <br/>{< br/> if (a [I] = temp) <br/>{< br/> int newtemp = R. next (minvalue, maxvalue); <br/> A [Index] = newtemp; <br/> return getnumber (A, index, minvalue, maxvalue, newtemp, R ); <br/>}< br/> return temp; <br/>}

 

As for efficiency, it should also be from one to three, decreasing one by one.

 

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.