2012年11月20日 星期二

Number Transformation


內容  
給你一個數字S,你可以將A轉換成B藉由加上一個XX是一個A的質因數(1A不考慮進去),現在你的工作就是找出最少需要轉換次數把S轉換成T
EX: 6 12
6->9->12   2

6->8-> 10->12 3
輸入說明 
每組測資都有兩的數字S (1<=S<=100) & T (1<=T<=1000)
兩個0代表結束,不用輸出任何數字 
輸出說明 

對於每個一個測資,除了0 0以外 ,請印出 “Case X: Y”X1開始算起,YS轉換到T的最小次數,若是無法轉換成功請輸出-1
範例輸入 help
6 12 
6 13 
0 0
範例輸出 
Case 1: 2 
Case 2: -1

2 則留言:

  1. Dim ori, aft, num(99), subb As Integer

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Min = 9999
    Do
    Input #1, ori, aft: casee = casee + 1
    subb = aft - ori
    If ori = aft And aft = 0 Then Exit Do
    For i = 2 To ori - 1
    For ii = 1 To i
    If i Mod ii = 0 Then mycount = mycount + 1
    Next
    If mycount < 3 And ori Mod i = 0 Then List1.AddItem i: mycount = 0
    Next
    For iii = 0 To List1.ListCount - 1
    If subb / List1.List(iii) < Min And (subb / List1.List(iii)) = Int(subb / List1.List(iii)) Then Min = subb / List1.List(iii)
    Next
    If Min = 0 Then Min = -1
    Print #2, "Case " & casee & ": " & Min
    Min = 0: List1.Clear: mycount = 0
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim tof As Boolean
    Dim ans As String
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    r = 1
    Do While Not EOF(1)
    Input #1, x, y
    If x = 0 And y = 0 Then End
    ReDim c(Val(x)): Min = 999
    For i = 2 To Val(x)
    tof = True: aans = Val(y) - Val(x): ftimes = 0
    For j = 2 To i
    If i <> j Then
    If i Mod j = 0 Then tof = False
    End If
    Next j
    If tof = True Then
    Do Until aans <= 0
    aans = aans - i
    ftimes = ftimes + 1
    Loop
    If aans < 0 Then ftimes = 0
    If ftimes < Min And ftimes <> 0 Then Min = ftimes
    End If
    Next i
    If Min <> 999 Then Print #2, "Case " & r & " : " & Min; Else Print #2, "Case " & r & " : " & -1
    r = r + 1
    Print #2,
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除