Private Sub Form_Activate() Open App.Path & "\in.txt" For Input As #1 Input #1, x ans = 0 For i = 1 To 9999 N = i For j = 2 To 5 If N Mod j = 0 Then N = N / j j = 1 End If If j = 3 Then j = j + 1 If j = 5 And N = 1 Then ans = ans + 1 Next j If ans = x Then Open App.Path & "/out.txt" For Output As #2 Print #2, i Close #2 Exit For End If Next i Close #1 End Sub
Public Sub Form_Load() Dim ans As Long Dim an As String Me.Hide Open App.Path & "/in.txt" For Input As #1 Open App.Path & "/out.txt" For Output As #2 Input #1, x k = 1: kk = 1 Do Until kk = x + 1 s = k Do m = 0 If s Mod 2 = 0 Then s = s \ 2: m = m + 1 If s Mod 3 = 0 Then s = s \ 3: m = m + 1 If s Mod 5 = 0 Then s = s \ 5: m = m + 1 Loop Until m = 0 If s = 1 Then kk = kk + 1 If x = (kk - 1) Then Print #2, k End If k = k + 1
小提示:
回覆刪除這個醜數,其實算法和質數也是類似。
先試寫找出第幾個質數的程式後,
和質數不同的是:
質數在2到本身減1之間,沒有因數。
醜數是找到因數後,再看是不是非2,3,5而已。
Private Sub Form_Activate()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Input #1, x
ans = 0
For i = 1 To 9999
N = i
For j = 2 To 5
If N Mod j = 0 Then
N = N / j
j = 1
End If
If j = 3 Then j = j + 1
If j = 5 And N = 1 Then ans = ans + 1
Next j
If ans = x Then
Open App.Path & "/out.txt" For Output As #2
Print #2, i
Close #2
Exit For
End If
Next i
Close #1
End Sub
Public Sub Form_Load()
回覆刪除Dim ans As Long
Dim an As String
Me.Hide
Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Input #1, x
k = 1: kk = 1
Do Until kk = x + 1
s = k
Do
m = 0
If s Mod 2 = 0 Then s = s \ 2: m = m + 1
If s Mod 3 = 0 Then s = s \ 3: m = m + 1
If s Mod 5 = 0 Then s = s \ 5: m = m + 1
Loop Until m = 0
If s = 1 Then
kk = kk + 1
If x = (kk - 1) Then Print #2, k
End If
k = k + 1
Loop
Close #2
Close #1
End
End Sub
11分5X秒