编写程序,找出由1,2,3,4四个不同数字组成的4位整数中的素数。

2024-11-22 03:20:56
推荐回答(2个)
回答1:

Dim a(4) As String

Private Sub Command1_Click()
List1.Clear
For i = 1 To 4
a(i) = Trim(Str(i))
Next i
For i1 = 1 To 4
For i2 = 1 To 4
If i2 <> i1 Then
For i3 = 1 To 4
If i3 <> i1 And i3 <> i2 Then
For i4 = 1 To 4
If i4 <> i1 And i4 <> i2 And i4 <> i3 Then
List1.AddItem a(i1) & a(i2) & a(i3) & a(i4)
End If
Next i4
End If
Next i3
End If
Next i2
Next i1
For i = 0 To List1.ListCount - 1
If 素数(CInt(List1.List(i))) Then List2.AddItem List1.List(i)
Next i
End Sub

Private Function 素数(m As Long) As Boolean
素数 = True
For i = 2 To Sqr(m)
If m Mod i = 0 Then
素数 = False
Exit For
End If
Next i
End Function

回答2:

将1,2,3,4,5这五个数字排成一排,最后一个数是奇数,且使得其中任意连续三个数之和都能被这三个数中的第一个数整除,那么满足要求的排法有几种