Home > Back-end >  Meet a random number of interview questions, visual without the optimal solution
Meet a random number of interview questions, visual without the optimal solution

Time:05-26

Accidentally see group of people in a job interview questions


Generate a random number (consisting of 0 ~ 9 Arabic Numbers, length is 6 to 15 characters)
Requirements
1. Cannot occur more than three consecutive same number (11234233456) is not allowed in
2. 3 consecutive Numbers cannot happen (123456, 123457, 2567891)
3. The generated string cannot be arithmetic series
4. The algorithm is efficient, less memory,


I'm no good way, about three steps

The first step to
Generate a random number
Generated a 64 - bit number, and then do random length segmentation,

The second step
Each string traversal from beginning to end, check whether there are three consecutive figures are the same, also check for three consecutive even Numbers,
If you have, the number of failure,

The third step
Ordinal traversal, judgment arithmetic series

Start out of jade, is there more efficient way to solve this problem? !

CodePudding user response:

The second step
Each string traversal from beginning to end, check whether there are three consecutive figures are the same, also check for three consecutive even Numbers,
If you have, the number of failure,

Don't fail!
Signature so many steps don't waste the
Isn't it wouldn't have failed swap
For example,
111234
A change to become
121314
Not just a matter of

CodePudding user response:

It is ok to block random
By random 6-15 digits
Then every random digits 1 as part of the
Each random inspection will not repeat the previous number will be and the preceding digit serial
It is ok

CodePudding user response:

Fyi:
//request 1: from give out of six different set of Numbers, from each group choose a number, and finally form a six digital array, 
//list of all the array
//at the same time, eliminate all is even, all for the odd number of arrays,
//out of four Numbers increasing continuous (such as 8,9,10,11) array,
//is as follows:
//the first number from the 12 Numbers:, 1,3,2,4,5,6,7,8,9,10,11,12
//the second number from the 17 of figures:, 8,7,5,9,6,4,11,10,12,13,3,14,17,2,16,15,18
//the third number from the 21 of figures:, 13,10,15,16,18,11,14,12,7,19,21,17,8,22,9,20,6,23,5,24,25
//the fourth Numbers from the 20 selected:, 22,23,18,20,16,17,15,21,14,26,19,24,27,25,12,13,11,10,28,29
//the fifth Numbers from the 19 selected:, 26,25,27,29,30,28,23,24,20,22,19,31,21,17,18,32,13,15,16
//6 Numbers selected: from the Numbers of the 14, 32,33,30,31,29,28,27,26,25,24,23,22,18,21
//require 2:
//please enter the first number range of options: (several Spaces can manually enter any interval number, up to 30)
//please enter the second number range of options: (several Spaces can manually enter any interval number, up to 30)
//please enter the number 3 options: (several Spaces can manually enter any interval number, up to 30)
//please enter the fourth number range of options: you can manually enter any several space interval number, up to 30)
//please enter the fifth number range of options: (several Spaces can manually enter any interval number, up to 30)
//please enter the number range of options: (6 can manually enter any several space interval number, up to 30)
//then press enter, the output to the cp. TXT
//require 3:
//that has a different number of the same set of data,
//, for example, the first number is 6, the second number or any other number can't again for 6; The second number 10, the third number or other can't again for 10, and so on...
//require 4:
//add a condition: produced in each of the array A1A2A3A4A5A6, requires A1 & lt; A2 & lt; A3 & lt; A4# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String. H>
Int v [6];
Int n [6]=,17,21,20,19,14 {12};
Int d [6] [30]={
,3,2,4,5,6,7,8,9,10,11,12 {1},
,7,5,9,6,4,11,10,12,13,3,14,17,2,16,15,18 {8},
13,10,15,16,18,11,14,12,7,19,21,17,8,22,9,20,6,23,5,24,25 {},
22,23,18,20,16,17,15,21,14,26,19,24,27,25,12,13,11,10,28,29 {},
26,25,27,29,30,28,23,24,20,22,19,31,21,17,18,32,13,15,16 {},
{32,33,30,31,29,28,27,26,25,24,23,22,18,21},
};
Int I, j, i0, i1, i2, i3, i2, i5, s;
Char ln [100], * t;
The FILE * f;
Int main () {
for (i=0; i<6; I++) {
Printf (" please enter the range of options: % d number for ", I + 1);
The fgets (ln, 100, stdin);
j=0;
T=strtok (ln, "");
While (1) {
If (NULL==t) break;
If (0==t [0]) continue;//jump out of an empty string when multiple Spaces between
D [I] [j]=atoi (t);
j++;
If (j>=30) break;
T=strtok (NULL, "");
}
N [I]=j;
}
F=fopen (" cp. TXT ", "w");
If (NULL==f) {
Printf (" can't generate cp. TXT! \n");
return 1;
}
Printf (" is being generated cp. TXT... ");
For (i0=0; I0 & lt; N [0]; I0 {+ +)
For (i1=0; I1 & lt; N [1]. {i1 + +)
For (i2=0; I2 & lt; N [2]. I2 + +) {
For (i3=0; I3 & lt; N [3]. I3 + +) {
For (i2=0; I4 & lt; N [4]; I2 + +) {
For (i5=0; I5 & lt; N [5]; I5 + +) {
V [0]=d [0] [i0];
V [1] [1]=d (i1);
V [2]=d [2] [i2];
V [3] [3]=d/i3.
V [4]=d [4] [i2];
V [5]=d [5] [i5];
If (0==(v [0] % 2)
& & [1] % 2 0==(v)
& & [2] % 2 0==(v)
& & [3] % 2 0==(v)
& & [4] % 2 0==(v)
& & 0==[5] % 2) (v) continue;//to exclude all is even
If (1==(v [0] % 2)
& & 1==[1] % 2 (v)
& & 1==[2] % 2 (v)
& & 1==[3] % 2 (v)
& & 1==[4] % 2 (v)
& & 1==[5] % 2) (v) continue; With odd//eliminate all
for (i=0; i<5; I++) {
For (j=I + 1; J<6; J++) {
If (v [I] [j]==v) goto NEXT;//that the same set of data are not the same number of
}
}
NEXT:
If (i> {
=5)for (i=0; i<5; I++) for (j=I + 1; J<6; J++) if (v [I] & gt; V [j]) {s=v [I]; V=v [I] [j]; V [j]=s; }//smallest
If ([0] (v + 1==v [1]
& & V + 1==v [1] [2]
& & V + 1==v [2] [3])
| | (n + 1==v [1] [2]
& & V + 1==v [2] [3]
& & V + 1==v [3] [4])
| | (n + 1==v [2] [3]
& & V + 1==v [3] [4]
& & V [4] [5] + 1==v)) continue;//out of four Numbers increasing continuous
Fprintf (f "% % % 2 d, 2 d, 2 d, 2 d %, % 2 d % 2 d \ n", v [0], v [1], v [2], v [3], v [4], v [5]);
}
}}}}}}
The fclose (f);
Printf (" \ n generated cp. TXT finished \ n ");
return 0;
}
//if the file in. TXT for the content of the
//1 2 3 4 5 6 7 8 9 10 11 12
//7 8 9 10 6 4 11 12 13 14 3 2 16 17 18
//13, 15, 16 18 November 14 12 July 19 and 17 August 22 September 20 June 23 24 25 5
//22 23 16 17 18 to 20 15 21 and 26 19 24 and 25 12 13 11 October 28, 29
//26 28 23 24 25 and 29 30 20 22 and 31 21 32 13 15 16 17 18
//30 31 32 33 27 and 28 26 23 24 25 22 18 21
//can enter commands inside the CMD window
//cp & lt; In the. TXT
//get request 1
the corresponding results//BTW: must I sixty percent! Hey hey! (^=^)

CodePudding user response:

The law of the arithmetic progression is just a name... There are other laws?nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related