[Visual Basic 6.0] 邏輯思考基礎入門 - GCD(最大公因數)

摘要:[Visual Basic 6.0] 邏輯思考基礎入門 - GCD(最大公因數)

[Visual Basic 6.0] 邏輯思考基礎入門 - GCD(最大公因數)

題目說明:


請寫一個程式求出2個數的GCD(最大公因數)

 

Sample Input

12 36

25 24

0 0

Sample Output

GCD(12,36)=12

GCD(25,24)=1


範例 Code:

 


Private Sub Form_Activate()
        Open App.Path & "\In.txt" For Input As #1 ' 讀入 In.txt 輸入檔
        Open App.Path & "\Out.txt" For Output As #2 ' 建立 Out.txt 輸出檔
    Do
        Line Input #1, InpData ' 讀入整段資料
        CaseData = Split(InpData, " ") ' 字串切割
        ' 判斷是否為為到底
        If CaseData(0) = CaseData(1) And Val(CaseData(0)) = 0 Then Exit Do
        
        Temp = 1 ' 預設最大公因數等於一
        
        For I = 1 To CaseData(0)
            If Val(CaseData(0)) Mod I = 0 And Val(CaseData(1)) Mod I = 0 Then Temp = I
        Next I
        
        Out = Out & "GCD(" & CaseData(0) & "," & CaseData(1) & ")=" & Temp & vbCrLf
    Loop
        Print #2, Out
        Close
        End
End Sub

 

#0xDe 從分享中學習

#Facebook:ProgrammerDe (https://www.facebook.com/MicrosoftDes) 有問題歡迎提問