他们都给了你好的答案,但我要给你一个可以让你电脑半天反应不过来的程序,得到的结果是一样的。但你放心,不会损坏你的电脑的,只是给你参考一下 最慢算法。
Private Sub Form_Click()
Dim i, j, k, l, N, U, M As Integer
If List1.ListCount = 0 Then
For N = 1000 To 9999
For i = 0 To 9
For j = 0 To 9
For k = 0 To 9
For l = 0 To 9
If N = i * 1000 + j * 100 + k * 10 + l Then
U = l * 1000 + k * 100 + j * 10 + i
If U > N * 2 Then
List1.AddItem (N & " " & U)
M = M + 1
End If
End If
Next
Next
Next
Next
Next
List1.AddItem ("共" & M & "组"), 0
End If
End Sub
Private Function F(m as integer)
dim n1 as integer,n2 as integer,n3 as integer,n4 as integer
dim num as string,Number_New as Integer
num=str(m)'把数值转为字符串
n1=val(mid(num,1,1))'千位数
n2=Val(mid(num,2,1))'百位数
n3=Val(Mid(num,3,1))'十位数
n4=Val(Mid(num,4,1))'个位数
Number_New=n4*1000+n3*100+n2*10+n1 '倒过来
If Number_New > m*2 Then Printf m;
End Function
'主调函数
Private Sub Command1_Click()
dim i as integer
For i= 1000 to 9999
f(i)
Next
End Sub
'函数F,可将一个整数的各个位倒序排列,形成一个新的数,当这个新数超过原数的2倍时返回True,否则返回False。函数兼容4位整数,也兼容其它1~9位的整数。
private function F(m as long) as boolean
dim i as long
dim m1 as long, m2 as long
m1=m
for i=1 to len(cstr(abs(m)))
m2=m2*10+(m1 mod 10)
m1=int(m1/10)
next i
if m2>(m*2) then F=true
end function
'调用
'在窗体里画一个Command1,再画一个List1用于显示
private sub Command1_Click()
dim i as long
List1.clear
for i=1000 to 9999
if F(i) then List1.additem i
next i
end sub
Private Function F(m As Integer) As Boolean
If m * 2 > Val(StrReverse(m)) Then
Exit Function
F = False
Else
F = True
End If
End Function
Private Sub Command1_Click()
Dim i As Integer
For i = 1000 To 9999
If F(i) Then List1.AddItem i
Next i
End Sub
Function f(m As Integer) As Boolean
Dim m1 As String
m1 = StrReverse(Str(m))
If Val(m1) / m > 2 Then f = True
End Function
Private Sub Command1_Click()
Dim i As Integer
For i = 1000 To 9999
If f(i) = True Then List1.AddItem i
Next
End Sub
简洁吧
Function F(ByVal A As String) As Boolean
Dim B As String
Dim i As Integer
For i = 0 To A.Length - 1
If B Is Nothing Then B = Microsoft.VisualBasic.Right(A, 1) Else B = B & Microsoft.VisualBasic.Right(Microsoft.VisualBasic.Left(A, A.Length - i), 1)
Next
If CInt(A) * 2 < CInt(B) Then F = True Else F = False
End Function
======使用方法
直接调用就可以返回判断了。而且使用的是字符型数字,这样范围可以是N位长度数。
例:程序中直接引用就可以知道结果了
if f(i)=true then messagebox.show(i & "此数符合判断要求!" else messagebox.show(i & "此数不符合判断要求!")