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.