Posted on 2006-07-07 14:34
飛仙 閱讀(272)
評論(0) 編輯 收藏 引用
????????? 已知兩個數字為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