Take a written test questions algorithm of Jingdong

Q (question)

  • In the past few days, see blog bigwigs see this title (from one blog of algorithm Jabber_YQ), the first question to you, can we try to analyze, look at the solution I or Jabber_YQ chiefs (for… I haven’t seen Jabber_YQ gangster…) :
Take a written test questions algorithm of Jingdong
the blog of from Jabber_YQ

A (answer)

  • First of all, when I look at this problem, I do not see it is to examine the logic of the meticulous or inspection skills……. So when it comes to examining the logic of it (skill of the words… , since it is such a problem, must be based on a certain parameter considerations.
  • I chose K, which is the threshold. Look at the code:
//wrong solution from me. int stoneHeapFilter (int n, int k) {static int = several n/k; static int severalSurplus = n%k; static = n/2 int halve; static int trisectionPoint = n/3; static = n%3 int trisectionPointSurplus //extract the particular value 1; if (k = = 1) {if (trisectionPointSurplus = = 0) {return trisectionPoint * else {return trisectionPoint 2;}}} * 2 + 1; //extract the particular value 2 if (k = = 2 & & K < halve) {if (trisectionPointSurplus = = 2) + 1 {return trisectionPoint else {return trisectionPoint;}}}; //the soul part of this answer. if (K > & = 3; & K < halve) {if (seve Ral = severalSurplus + 1)} {{return several; else while (several > severalSurplus) {several = 1; severalSurplus = k;}}} return several; //extract the particular value between "n/2... N". If (K > halve = & & = k; < n) {return 1 //dispose error case.;} if (K > n) {cout< < error: can't get k bigger than n. < < endl;} return 0;}
  • I believe you can understand (my clear structure of code structure), so it is not so detailed notes. K = 1 or 2, using the “3 + 1 = 2” in this special case, so directly to the 1, 2 as a whole segmentation n; K belongs to 3, n/2), the direct use of K n segmentation, the relationship between the quotient and remainder of the need to deal with the number of stack. K (n/2, n), need to pay attention to the odd half (Int). K error greater than n.
  • Here, if it is admitted to careful thinking ability, they have not too small (it took me two hours to write the code from debugging to finish this blog…… The written examination ended early). Hope to see the big brother’s advice, thank you.

C (comment)

  • Zhong Sanming gangster comments on the first floor.
//wise solution from Sanming mogul. int split (int n, int k) {int RST = n/ (2*k + 1); return n% (2*k +1 >); k = rst*2 + 1?: rst*2;}
  • First of all, analyze the basic idea. Where RST is K and k+1 for a set of split n. This will get the remainder n% (2*k +1), and then through the remainder of the comparison with the threshold K to return the last heap: the remainder is greater than the threshold can also be divided into a pile (k), less than enough to separate a pile of words (K).
  • Ha-ha。 To show that my method is wrong. Summary: I want to take K and 1 as a group to split the N, so that the number of K, i.e. (k+1) the number of K and k+1; Sanming chiefs to a group of split n, so that the number of 2k+1; when the Sanming chiefs only 2k+1 stone scored two runs when I use 2 * (k+1) block (n the greater the gap is more obvious, ha ha) the difference we should be able to see… Perhaps this is the big brother and Meng new bar…… Thanks again to bell Sanming chiefs and young you weak brother, thank you.