在數(shù)學中也有這樣一類數(shù)字有這樣的特征,成為回文數(shù)(palindrome number)。
巢湖網(wǎng)站建設公司創(chuàng)新互聯(lián),巢湖網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為巢湖超過千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的巢湖做網(wǎng)站的公司定做!
設n是一任意自然數(shù)。若將n的各位數(shù)字反向排列所得自然數(shù)n1與n相等,則稱n為一回文數(shù)。例如,若n=1234321,則稱n為一回文數(shù);但若n=1234567,則n不是回文數(shù)。
注意:
1.偶數(shù)個的數(shù)字也有回文數(shù)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,為假,就退出循環(huán),
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 ("是回文數(shù)")
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 "是回文數(shù)"
Else
Return "不是回文數(shù)"
End If
End If
End Function
'判斷一個整數(shù)的奇偶
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內(nèi)部,當條件不滿足將導致函數(shù)無返回。
以下代碼僅供參考:
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