How to generate 1-100 between 100 random numbers, but here is fine, in Ctrip interview. NET is no written:-)
If this is the first time you've seen this topic, maybe you have a lot of ideas.
1: First randomly select a number from the original array, and then remove the number from the array, and then select, then remove, repeat 99 times, to solve.
We know that the complexity of removing an element from an array is O (n), then randomly selecting N numbers, and its complexity is O (N2).
2: With the hash as the middle filter layer, because in the array, we use random number, perhaps random number in multiple random may have duplication, so need to use hash to judge, if repeated in the hash, then continue to produce random numbers, until not repeated, of course, this complexity is not easy to say, You have to see random numbers randomly random, good, O (N), unlucky words no Limit ~
3: As the title said, many of the problems we can find in the real life, after all, many things are derived from the reality, and abstract in reality, such as this topic in real life, you can correspond to the "washing cards", in the algorithm is also called "Shuffle Principle", We know that the way to wash cards is a random exchange of poker position, also known as "Cut card", when you cut a lot of times, our poker can be considered to be enough chaos, complexity has become an O (N), with the code to achieve this is the case.
<1> first generates 52 cards in an orderly fashion and then puts them in an orderly array.
<2> from 1-52 randomly generated a number, and then the current number of positions followed the number of locations to exchange, repeat 52 times, our cards can be considered enough chaos.
4: Code implementation
<1> first defines the data structure of a card, defining a "suit" and a "number"
<summary>
///Specific poker
///</summary> public
class cards
{public
char suit;
public string num;
}
<2> generate 52 cards in an orderly fashion
<summary>
///Open
///</summary> public
void Newcard ()
{for
(int i = 1; I <= card.l Ength; i++)
{
var suit = (i-1) + 3;
var num = i%;
string temp;
Switch (num)
{case
1:temp = "A";
Case 11:temp = "J"; break;
Case 12:temp = "Q"; break;
Case 0:temp = "K"; break;
Default:temp = num. ToString (); break;
CARD[I-1] = new Card ()
{
suit = (char) suit,
num = temp
};
}
<3> then is cut the card, just also said the idea, is to take the position of random number and the position of the current I exchange, but when it comes to the exchange of "bubble sort", may be poisoned too deep (┬_┬), do not know you feel it.
<summary>
///Shuffle
///</summary> public
void Shuffle ()
{for
(int i = 0; i < card. Length; i++)
{
var rand = new Random ((int) DateTime.Now.Ticks). Next (0, card. Length);
Because the random number is pseudo, true random number is to deal with the hardware, so here set the stay 1ms
System.Threading.Thread.Sleep (1);
var temp = Card[rand];
Card[rand] = Card[i];
Card[i] = temp;
}
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/