Tricky Google interview question

前端 未结 21 1676
花落未央
花落未央 2020-12-22 15:39

A friend of mine is interviewing for a job. One of the interview questions got me thinking, just wanted some feedback.

There are 2 non-negative integers: i and j. Gi

21条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-22 16:21

    Why not try looking at this from the other direction. Use a counter to test the possible answers against the original formula. Sorry for the pseudo code.

    for x = 1 to n
    {
      i=j=0
      y=x
      while ( y > 1 )
      {
        z=y
        if y divisible by 2 then increment i and divide y by 2
        if y divisible by 5 then increment j and divide y by 5
    
        if y=1 then print i,j & x  // done calculating for this x
    
        if z=y then exit while loop  // didn't divide anything this loop and this x is no good 
      }
    }
    

提交回复
热议问题