发布网友 发布时间:2022-04-23 03:40
共3个回答
热心网友 时间:2023-09-27 06:33
第一个问题
'是否是素数
Private Function s(ByRef x As Integer) As Boolean
Dim i As Integer
For i = 2 To x - 1
If x Mod i = 0 Then
s = False
Exit Function
End If
Next
s = True
End Function
'转后一半数, 如:15877851 把7851转成1587
Private Function U(ByRef s As String) As Long
Dim l As Integer
Dim pp As String
Dim i As Integer
l = Len(s)
For i = l To 1 Step -1
pp = pp + Mid(s, i, 1)
Next
U = Val(pp)
End Function
'在listbox 控件里显示 我是从3开始判断
Dim i As Integer
Dim L1 As Integer
Dim j As Integer
Dim str1 As String
For i = 3 To 1000
If s(i) Then
L1 = Len(Trim(Str(i))) \ 2
If Val(Left(Trim(Str(i)), L1)) = U(Right(Trim(Str(i)), L1)) Then
List1.AddItem i
End If
End If
Next
关于第二个问题
在你原题里,有几个变量很是看不明白,是你抄的问题还是原题就是这样,如果原题是这样的话,真是无语了
Private Function f(ByRef x As Integer) As Long
Dim t As Long
Dim i As Integer
t = 1
For i = 1 To x
t = t * i
Next
f = t
End Function
Dim m As Integer
Dim n As Integer, i As Integer, s As Long, t As Integer
m = Val(Text1.Text)
n = Val(Text2.Text)
s = 0
For i = m To n
t = f(i)
s = s + t
Next
LbResult.Caption = s
End Sub
以上测试通过
热心网友 时间:2023-09-27 06:34
1、
'判断素数的
Private Function Sushu(ByVal a As Integer) As Boolean
Dim i As Integer
Dim t As Boolean = True
For i = 2 To a - 1
If a Mod i = 0 Then
t = False
Exit For
End If
Next
Return t
End Function
'判断回文的
Private Function Huiwen(ByVal a As Integer) As Boolean
Dim i As Integer
If CStr(a).Length > 1 Then
Dim t As Boolean = True
For i = 1 To CStr(a).Length / 2
If CStr(a).Substring(i - 1, 1) <> CStr(a).Substring(CStr(a).Length - i, 1) Then
t = False
Exit For
End If
Next
Return t
End If
End Function
‘调用事件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To 33
If Sushu(i) And Huiwen(i) Then
MessageBox.Show(i & "是回文素数")
Else
MessageBox.Show(i & "不是回文素数")
End If
Next
End Sub
2、i=m To n
t=f(i) ’不过这里应该先声明一下t吧?
s=s+t
t=t*i
f=t
热心网友 时间:2023-09-27 06:34
怎么都喜欢提问,不喜欢搜索找代码呢?