摘要:[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) 有问题欢迎提问
原文:大专栏 [Visual Basic 6.0] 逻辑思考基础入门 - GCD(最大公因数)
原文地址:https://www.cnblogs.com/chinatrump/p/11496822.html
时间: 2024-10-12 09:02:29