Algorithm for interview topics of Microsoft companies

zhaozj2021-02-16  50

It is said to be a microsoft's interview question.

Original topic describes:

It is known that two numbers are 1 ~ 30, and the one knows two times, and B knows the stream of two, Aeration B: "Do you know that it is the number?" Said: "Don't know." BRIA: "Do you know that the two numbers?" Said A: "Don't know." So, B said: "Then I know" After the armor also said: "Then I know" these two numbers are 'what?

The following uses VB. NET implementation:

DIM NUM, SUM, PRODUCT AS INT32 DIM PRODUCT1 () () AS INT32 DIM I, M, N, SUM1 (3) () AS INT32

Private sub mymain () product1 = Nothing Num = cint (me.textbox1.text) getsum1 () getProduct1 () for m = 1 to num for n = m = 1 to num for n = m to num if Sumonly (m, n) or productOnly (m, n) Then Goto nextItem 'embarrassment with a goto sum = m n product = m * n' A and generated in total (N-2) is the only product if SUMTOPRODUCT_N_2 (SUM) <2 Then Goto nextItem ' The product produced by B is there and only one satisfied 1, not only the only and 2, and the resulting stream has the most (n -2) is the only product 'and the rest is satisfied with 1, not unique and 2, there is N-1 Unique IF ProCucttosum (Product) THEN MSGBOX (M.TOString () & "& n.tostring ()) End ifnextItem: Next

NEXT

End Sub Private Sub getSum1 () 'Generates unique and saved in array Redim Sum1 (0) (1) SUM1 (0) (0) = 1 Sum1 (0) (1) = 1 redim Sum1 (1) (1) Sum1 (1) (0) = 1 Sum1 (1) (1) = 2 Redim Sum1 (2) (1) Sum1 (2) (0) = Num - 1 SUM1 (2) (1) = Num Redim Sum1 (3 ) (1) SUM1 (3) (0) = Num Sum1 (3) (1) = Num End Sub PRIVATE FUNCTION SUMONLY (BYVAL N1 AS INT32, BYVAL N2 AS INT32) AS Boolean 'Judging whether it is unique and DIM I as Int32 For i = 0 to 3 if n1 = sum1 (i) (0) ANDALSO N2 = SUM1 (i) (1) THEN RETURN TRUE NEXT RETURN FALSE End Function Private Sub getProduct1 () "Generates unique accumulation and saved in array DIM TMP (Num * Num) () AS INT32 for M = 1 to Num '?????????????????????????????????????????????? ??? Dim Meme () AS INT32 = TMP (M * N) if me is nothing the redim me (2) Else Redim preserve meme (meme.length 1) endiff

MEME (MEME.LENGTH - 1) = m MEME (MEME.LENGTH - 2) = n meme (0) = 1 TMP (m * n) = meme meme = Nothing Next Next for i = 1 to num * Num if not NOT TMP (i) is nothing andalso tmp (i) (0) = 1 THEN for M = 1 to TMP (i) .getupperbound (0) Step 2 if Product1 is nothing the redim product1 (0) redim products (0) (1 (Product1.Length) Redim Product1 (Product1.Length - 1) (1) Endiffuct1 (Product1.Length - 1) (0) = TMP (i) (M) Product1 (Product1.Length - 1 ) (1) = TMP (i) (m 1) Next End End End End End Sub Private Function Productonly (Byval N1 AS INT32, BYVAL N2 AS INT32) AS Boolean 'Judging whether it is the only product DIM I as Int32 for i = 0 To Product1.getupperboun D (0) IF n1 = product1 (i) (1) Andalso N2 = Product1 (i) (0) THEN RETURN TRUE IF N1 = Product1 (i) (0) ANDALSO N2 = Product1 (i) (1) THEN RETURN TRUE Next RETURN FALSE End Function Private Function SUMTOPRODUCT_N_2 (Byval Sum as INT32) AS INT32 'Jia A and Generated Rehabilitation Most (N-2) is the only product DIM N as INT32 = CINT (SUM / 2 - 0.2) DIM i , M as int32 for i = 1 to n if Productonly (i, sum - i) THEN M = 1 Next Return N - M End Function Private Function Procucttosum (Byval Product as INT32) AS Boolean ' There is only one satisfaction 1, not the only one and 2, and the result of the result is most (N-2)

转载请注明原文地址:https://www.9cbs.com/read-21955.html

New Post(0)