问题:
西索罗教授与一逻辑朋友同乘62路公共汽车。他们买了两张票,票的号码都是五位数,且两张票的号码相连。西索罗教授把两张票上的十个数字加在一起,告诉朋友十个数字之和正好是62。他的朋友问是否其中一张票的五个数字之和恰好是35,西索罗教授如实回答了,然后他的朋友就正确地推算出了两张票的号码。请问,这两个号码是多少(答案唯一)?
解答:
<%@ Page Language="VB" %>
<script runat="server">
Sub Page_Load()Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Dim minh As Integer = 10000
Dim maxh As Integer = 99999
Dim a, b, c, i As Integer
i = c = 0
'假设其中一张票的五个数字之和等于35的情况
For i = 10000 To 99999
If shs(i) + shs(i + 1) = 62 Then
'其中一张的五个数字之和等于35
If shs(i) = 35 Or shs(i + 1) = 35 Then
a = i
b = i + 1
c = c + 1
End If
End If
Next i
'判断结果是否为唯一
If c = 1 Then
Vote_1.Text = a
Vote_2.Text = b
Exit Sub
End If
'假设其中一张票的五个数字之和不等于35的情况
i = c = 0
For i = 10000 To 99999
If shs(i) + shs(i + 1) = 62 Then
'两张都不等于35
If Not shs(i) = 35 And Not shs(i + 1) = 35 Then
a = i
b = i + 1
c = c + 1
End If
End If
Next
'判断结果是否为唯一
If c = 1 Then
Vote_1.Text = a
Vote_2.Text = b
Exit Sub
End If
End Sub
Private Function shs()Function shs(ByVal x) As Integer
'计算票的五个数字之和
Dim d As Integer = 0
Dim a As Integer
For a = 1 To 5
d = d + Mid(x, a, 1)
Next
Return d
End Function
</script>
<html>
<body>
<form id="form1" runat="server">
<asp:label id="Vote_1" runat="server"/>
<asp:label id="Vote_2" runat="server"/>
</form>
</body>
</html>
<script runat="server">
Sub Page_Load()Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Dim minh As Integer = 10000
Dim maxh As Integer = 99999
Dim a, b, c, i As Integer
i = c = 0
'假设其中一张票的五个数字之和等于35的情况
For i = 10000 To 99999
If shs(i) + shs(i + 1) = 62 Then
'其中一张的五个数字之和等于35
If shs(i) = 35 Or shs(i + 1) = 35 Then
a = i
b = i + 1
c = c + 1
End If
End If
Next i
'判断结果是否为唯一
If c = 1 Then
Vote_1.Text = a
Vote_2.Text = b
Exit Sub
End If
'假设其中一张票的五个数字之和不等于35的情况
i = c = 0
For i = 10000 To 99999
If shs(i) + shs(i + 1) = 62 Then
'两张都不等于35
If Not shs(i) = 35 And Not shs(i + 1) = 35 Then
a = i
b = i + 1
c = c + 1
End If
End If
Next
'判断结果是否为唯一
If c = 1 Then
Vote_1.Text = a
Vote_2.Text = b
Exit Sub
End If
End Sub
Private Function shs()Function shs(ByVal x) As Integer
'计算票的五个数字之和
Dim d As Integer = 0
Dim a As Integer
For a = 1 To 5
d = d + Mid(x, a, 1)
Next
Return d
End Function
</script>
<html>
<body>
<form id="form1" runat="server">
<asp:label id="Vote_1" runat="server"/>
<asp:label id="Vote_2" runat="server"/>
</form>
</body>
</html>