內容 :
給你一個數字S,你可以將A轉換成B藉由加上一個X,X是一個A的質因數(1跟A不考慮進去),現在你的工作就是找出最少需要轉換次數把S轉換成T
EX: 6 12
6->9->12 2次
6->8-> 10->12 3次
EX: 6 12
6->9->12 2次
6->8-> 10->12 3次
輸入說明 :
每組測資都有兩的數字S (1<=S<=100) & T (1<=T<=1000),
兩個0代表結束,不用輸出任何數字 。
兩個0代表結束,不用輸出任何數字 。
輸出說明 :
對於每個一個測資,除了0 0以外 ,請應出 “Case X: Y”,X從1開始算起,Y為S轉換到T的最小次數,若是無法轉換成功請書出-1
範例輸入 :
6 12
6 13
0 0
範例輸出 :
Case 1: 2
Case 2: -1
Dim ans As String
回覆刪除Public Sub Form_Load()
Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Me.Hide
Do
ans = ""
Input #1, x, y
If x = 0 And y = 0 Then Exit Sub
For i = 2 To x - 1
If x Mod i = 0 Then ans = ans & " " & i
Next i
an = Split(ans)
Min = 99
For i = 1 To UBound(an)
step = x: k = 0
Do Until step >= y
step = step + an(i)
k = k + 1
Loop
If Min > k And step = y Then Min = k
If Min = 99 Then Min = -1
Next i
Print #2, Min
Loop
End
Close #2
Close #1
End Sub
by yung
Dim ans As String
回覆刪除Dim zzz As Boolean
Public Sub Form_Load()
Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Me.Hide
Do
ans = ""
Input #1, x, y
If x = 0 And y = 0 Then Exit Sub
For i = 2 To x - 1
If x Mod i = 0 Then
zzz = False
For j = i + 1 To i ^ 0.5
If i Mod j = 0 Then
zzz = True
Exit For
Next j
If zzz = False Then
ans = ans & " " & i
End If
Next i
an = Split(ans)
Min = 99
For i = 1 To UBound(an)
step = x: k = 0
Do Until step >= y
step = step + an(i)
k = k + 1
Loop
If Min > k And step = y Then Min = k
If Min = 99 Then Min = -1
Next i
Print #2, Min
Loop
End
Close #2
Close #1
End Sub
BY yung
沒看到質因數