在数学中也有这样一类数字有这样的特征,成为回文数(palindrome number)。
巢湖网站建设公司创新互联,巢湖网站设计制作,有大型网站制作公司丰富经验。已为巢湖超过千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的巢湖做网站的公司定做!
设n是一任意自然数。若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数。例如,若n=1234321,则称n为一回文数;但若n=1234567,则n不是回文数。
注意:
1.偶数个的数字也有回文数124421
如:1234321
Len(s) / 2=7/2=3.5
k = 1,Mid(s, k, 1)=1,Mid(s, (Len(s) - k + 1), 1)=Mid(s, (7 -1 + 1), 1)=1
k = 2,Mid(s, k, 1)=2,Mid(s, (Len(s) - k + 1), 1)=Mid(s, (7 -2 + 1), 1)=2
……
如果k = 3,为假,就退出循环,
If k Len(s) / 2 Then
k=3 Len(s) / 2 =3.5
在WinForm项目的窗体上添加一个按钮和文本框,然后把下面的代码粘贴上去就可以了。
Public Function lsH(ByVal n As String) As String
Dim s1 As String = ""
Dim s2 As String = ""
Dim i As Integer = 0
Dim j As Integer
If n.Length = 1 Then
Return ("是回文数")
Else
If Tell_JiOu(n.Length) = 1 Then
j = (n.Length + 1) / 2
s1 = Mid(n, 1, j)
Else
j = n.Length / 2 + 1
s1 = Mid(n, 1, j - 1)
End If
For i = n.Length To j Step -1
s2 = s2 + Mid(n, i, 1)
Next
If s1 = s2 Then
Return "是回文数"
Else
Return "不是回文数"
End If
End If
End Function
'判断一个整数的奇偶
Public Function Tell_JiOu(ByVal k As Integer) As Integer
If k Mod 2 = 0 Then
Return 2
Else
Return 1
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Button.Text = lsH(TextBox1.Text)
End Sub
你的i = Len(Str(a))会出问题。
If i 1 Then
Return True
End If
这个返回在if内部,当条件不满足将导致函数无返回。
以下代码仅供参考:
Function IsH(ByVal a As Long) As Boolean
Dim i As Long, j As Long, t As String
t = a.ToString
i = t.Length
Select Case i
Case 1
Return True
Case 2
If Mid(t, 1, 1) = Mid(t, 2, 1) Then Return True Else Return False
Case 3
If Mid(t, 1, 1) = Mid(t, 3, 1) Then Return True Else Return False
Case Else
For j = 1 To i \ 2
If Mid(t, j, 1) Mid(t, i - j + 1, 1) Then Return False
Next j
End Select
Return True
End Function
Function IsH(n) As Boolean
Dim s As String
s = CStr(n)
IsH = s = StrReverse(s)
End Function
Function IsH2(n) As Boolean 'Mid版
Dim s As String, i As Integer, k As Integer, rt As Boolean
s = CStr(n)
k = Len(s) \ 2
rt = True
For i = 1 To k
If Mid(s,i,1)Mid(s,k-i+1,1) = False Then
rt = False
Exit For
End If
Next
IsH2 = rt
End Function