据说属微软公司的面试题.
原题目描述:
已知两个数字为1~30的,甲知道两数只和,乙知道两数之积,甲问乙:“你知道是 那两个数吗?”乙说:“不知道”。乙问甲:“你知道是那两个数吗?”甲说:“也 不知道”。于是,乙说 :“那我知道了”随后甲也说:“那我也知道了”这两个数是'什么?
以下用VB。NET实现:
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 To NUM If SumOnly(m, n) Or ProductOnly(m, n) Then GoTo NextItem '不好意思用了个GOTO SUM = m + n PRODUCT = m * n '甲的和产生的积中最多有(n -2)个是唯一积 If SUMtoPRODUCT_N_2(SUM) < 2 Then GoTo NextItem '乙的积产生的和中有且只有一个满足1、不是唯一和 2、和产生的积中最多有(n -2)个是唯一积 '并且其余的和均满足 1、不是唯一和 2、有n-1个唯一积 If PROCUCTtoSUM(PRODUCT) Then MsgBox(m.ToString() & " " & n.ToString()) End If NextItem: Next
Next
End Sub Private Sub GetSum1() '产生唯一和并保存在数组中 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 '判断是否为唯一和 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() '产生唯一积并保存在数组中 Dim tmp(NUM * NUM)() As Int32 For m = 1 To NUM '???????????????? For n = m To NUM '?????????????? Dim meme() As Int32 = tmp(m * n) If meme Is Nothing Then ReDim meme(2) Else ReDim Preserve meme(meme.Length + 1) End If
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 tmp(i) Is Nothing AndAlso tmp(i)(0) = 1 Then For m = 1 To tmp(i).GetUpperBound(0) Step 2 If Product1 Is Nothing Then ReDim Product1(0) ReDim Product1(0)(1) Else ReDim Preserve Product1(Product1.Length) ReDim Product1(Product1.Length - 1)(1) End If Product1(Product1.Length - 1)(0) = tmp(i)(m) Product1(Product1.Length - 1)(1) = tmp(i)(m + 1) Next End If Next End Sub Private Function ProductOnly(ByVal N1 As Int32, ByVal N2 As Int32) As Boolean '判断是否为唯一积 Dim i As Int32 For i = 0 To Product1.GetUpperBound(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 '甲的和产生的积中最多有(n -2)个是唯一积 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 '乙的积产生的和中有且只有一个满足1、不是唯一和 2、和产生的积中最多有(n -2)个是唯一积 '并且其余的和均满足 1、不是唯一和 2、有n-1个唯一积 Dim tmp()(), i, m, n As Int32 '1、分解积看能产生多少个和 For i = 1 To CInt(Math.Sqrt(PRODUCT) - 0.4) If PRODUCT Mod i = 0 Then If tmp Is Nothing Then ReDim tmp(0) ReDim tmp(0)(2) Else ReDim Preserve tmp(tmp.Length) ReDim Preserve tmp(tmp.Length - 1)(2) End If tmp(tmp.Length - 1)(2) = PRODUCT / i tmp(tmp.Length - 1)(1) = i If Not SumOnly(tmp(tmp.Length - 1)(1), tmp(tmp.Length - 1)(2)) And SUMtoPRODUCT_N_2(i + PRODUCT / i) >= 2 Then '和不为唯一和,且和产生的积中支多有n-2个是唯一积 tmp(tmp.Length - 1)(0) = 1 End If If SumOnly(tmp(tmp.Length - 1)(1), tmp(tmp.Length - 1)(2)) Then '唯一和 tmp(tmp.Length - 1)(0) = 3 End If If Not SumOnly(tmp(tmp.Length - 1)(1), tmp(tmp.Length - 1)(2)) And SUMtoPRODUCT_N_2(i + PRODUCT / i) = 1 Then '不是唯一和,但是有n-1个唯一积 tmp(tmp.Length - 1)(0) = 2 End If End If Next Dim count As Int32 = 0 For i = 0 To tmp.Length - 1 If tmp(i)(0) = 0 Then Return False If tmp(i)(0) = 1 Then count += 1 Next If count <> 1 Then Return False Return True End Function 
|