1.要怎么输入啊,没写明,输入的数字作为做大还是最小值也没写,还是直接输出好了
Private Sub Form_Click()
For i = 100 To 999
If i = (i \ 100) ^ 3 + ((i \ 10) Mod 10) ^ 3 + (i Mod 10) ^ 3 Then Print i
Next
End Sub
2.While-wend如下:
Function IsPrime(value As Long) As Boolean
Dim i As Long
For i = 2 To Sqr(value)
If value Mod i = 0 Then IsPrime = False: Exit Function
Next i
IsPrime = True
End Function
Private Sub Form_Click()
Dim i As Long, a As Long
a = InputBox("输入整数")
i = 2
While i < a
If IsPrime(i) Then Print i
i = i + 1
Wend
End Sub
for-next如下:
Function IsPrime(value As Long) As Boolean
Dim i As Long
For i = 2 To Sqr(value)
If value Mod i = 0 Then IsPrime = False: Exit Function
Next i
IsPrime = True
End Function
Private Sub Form_Click()
Dim i As Long, a As Long
a = InputBox("输入整数")
For i = 2 To a
If IsPrime(i) Then Print i
Next
End Sub
3.
Private Sub Form_Click()
Randomize
n = InputBox("输入正整数")
For i = 1 To n
For j = 1 To n
Print 1 + Int(Rnd() * 99);
Next
Print
Next
End Sub
4.
Private Function gys(m As Long, n As Long) As Long
'求出两个数的最大公约数
gys = m Mod n
Do While gys <> 0
m = n
n = gys
gys = m Mod n
Loop
gys = n
End Function
Function gbs(a As Long, b As Long) As Long
'求出两个数的最小公倍数
Dim X As Long, Y As Long, K As Long, 最大公约数 As Long
X = IIf(a >= b, a, b) 'x存入最大值
Y = IIf(a <= b, a, b) 'y 存入最小值
Do '辗转相除法
K = X Mod Y: If K = 0 Then Exit Do
X = Y: Y = K
Loop
最大公约数 = Y
gbs = a * b / 最大公约数
End Function
Private Sub Form_Click()
Dim a As Long, b As Long
a = InputBox("输入数字1")
b = InputBox("输入数字2")
MsgBox "最大公约数:" & gys(a, b) & vbNewLine & "最小公倍数:" & gbs(a, b)
End Sub