Liuxxoo8818:53 2016.01.24 questions

A programming interview questions, as long as the pseudo code can be written on the

Assuming that there are two functions rand3 () can generate random 012, rand5 () can generate random 01234, and now you use it to write a function rand7 (), the number of random generated 0~6

Sort by number
Qq_27183003   00:02 2016.01.26

Not so perfect:

``````Rand7 int ()
{
Sum int;
Switch (rand3 ())
{
0: case
Sum=rand5 (); //0,1,2,3,4
Break;
1: case
+5 () sum=rand5; //5,6,7,8,9
Break;
2: case
Do
{
+10 () sum=rand5; //10,11,12,13,14
}while (sum! =14);
Break;
}
//0,1,2,3,4,5,6; sum%=7
Sum return;
}
``````
Caoluchao Reply Chely_Yi: of course not, 14 have been canceled, as well as the probability of the inside?????
Chely_Yi Reply Lin Shen: there should be no problem like taking the ball from the box to get 14 on the back and then take a time to get the other ball probability or 1/5
Leilba Reply caozhy: such words 0~9 to take the probability is 1/ (3*5), and the probability of 10~13 access is 1/ (3*4), ranging from the probability of the
Caozhy Ysuwood: back to the right, and I almost thought.
Qq_27183003 The logic is wrong: while (sum==14);
Leilba   09:07 2016.01.26

Optimized method

``````Mod / / from the homogeneous 0~20 inside
Rand7 int () {

Sum int = 0;
For (i=0 i<7; int; i++) {
Rand3 +i*3 () sum+=;
}
Sum%7 return;
}

Mod / / from the homogeneous 0~34 inside
Rand7 int () {

Sum int = 0;
For (i=0 i<7; int; i++) {
Sum = rand5 (+i*5);
}
Sum%7 return;
}
``````
Caoluchao Sum there are 2187 different values, if the 2187 is not a multiple of 7, so it is not a uniform probability between 0~6
U012414995 Positive solution, Diao fried days of the code, good and simple to say.
Leilba Two choose one
Qq_15622913   10:03 2016.01.28

Using an array of preferences
Rand7 int () {
A[][]={int
0,1,2,
3,4,5,
6,0,1,
2,3,4,
5,6,7}
Row=rand5 int;
Col =rand3 int;
Rand7=7 int;
While (rand7=7) {
Rand7=a[row][col];
}
Rand7 return;
}

Leilba A very good way, clear thinking, the efficiency is very high
Galiniur0u   16:08 2016.01.28

Rand7 int ()
{
Rand3 return () + rand5 ();
}

YnuCyan   19:08 2016.01.24

Rand7=rand3+rand5
Rand7=rand3+rand3+rand3
Rand7=rand3*2+rand3
Rand7=rand3*3-rand5
There should be some,

Caozhy      19:13 2016.01.24
``````Rand7 int ()
{
X5 int = 7;
While (x5 > 6)
{
X3 int = 2;
While (X3 > 1)
{
X5 = rand (5);
If (X3 = = 1) X5 = X5 + 5;
If (x5 < 7) X5 return;
}
}
}
``````
Moluth   20:09 2016.01.24

A=rand5+rand5+ int... +rand5 + rand3+... +rand3
A%8 return;

The number of their own decisions.

Qq_27183003   20:10 2016.01.24

In order to make the probability of random numbers equal:

``````Rand7 int ()
{
Sum=0 int;
Sum+=rand3 (); //0,1,2
+3 () sum+=rand3; //3,4,5
+6 () sum+=rand3; //6,7,8
+9 () sum+=rand5; //9,10,11,12,13
//0,1,2,3,4,5,6; sum%=7
Sum return;
}
``````
Caozhy Reply ysuwood: ha ha, do not quite right. This is not evenly distributed.
Qq_27183003 No, there's a mistake. Modify modify.
Lovingning   22:22 2016.01.24

The first dice have three possible, the second dice have five possible permutations and combinations there are fifteen may take one of the seven or fourteen, left abandoned and not on the line, as long as the guarantee that one in seven or fourteen one, anyway, is computer operation, what a waste of resources

Zhaoyangnierwu   15:34 2016.01.25

Rand7 int ()
{
X5 int = rand5 ();
X3 int = rand3 ();
While (==x3 x5) {
X3 int = rand3 ();

}
X5+x3 return;
}

Qq_15622913 This can not be generated 0