內容 :
給你一個數字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
回覆刪除Private Sub Form_Load()
Me.Hide
Dim a(100), d(100), min As Integer
Dim b As Boolean
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
ans = 0
Do
Input #1, n, m
If n = 0 And m = 0 Then Exit Do
j = 0
For i = 2 To n - 1
If n Mod i = 0 Then j = j + 1: a(j) = i
Next
c = 0
For i = 1 To j
b = True
For k = 2 To a(j) - 1
If a(j) Mod k = 0 Then b = False
Next
If b = True Then c = c + 1: d(c) = a(i)
Next
e = m - n
min = 100
For i = 1 To c
If e Mod d(i) = 0 Then
If (e / d(c)) < min Then min = (e / d(c))
End If
Next
ans = ans + 1
If min <> 100 Then Print #2, "Case " & ans & ":" & min Else: Print #2, "Case " & ans & ": -1"
Loop Until n = 0 And m = 0
Close
Close
End
End Sub
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Dim f As Boolean
Dim a(100) As Integer
X = 0
Do
Input #1, S, T
If S = 0 And T = 0 Then Exit Do
c = 0
For i = 2 To T - 1
If S Mod i = 0 Then
For j = 2 To i - 1
f = True
If i Mod j = 0 Then f = False: Exit For
Next j
If i = 2 Then f = True
If f = True Then c = c + 1: a(c) = i ': Print #2, a(c)
End If
Next i
n = T - S
y = T
For k = 1 To c
If n Mod a(k) = 0 Then
If (n / a(c)) < y Then y = (n / a(c))
End If
Next k
X = X + 1
If y <> T Then
Print #2, "Case" & X & ":" & y
Else
Print #2, "Case" & X & ":" & "-1"
End If
Loop Until S = 0 And T = 0
Close #2
Close #1
End
End Sub