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
在数学中也有这样一类数字有这样的特征,成为回文数(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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)