Dim ta, tb Private Sub Form_Load() Dim S1 As String, S2 As String Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, S1 Input #1, S2 SANS = Divi((S1), (S2)) Print #2, "最大公因數:" & SANS End End Sub Function Divi(a As String, b As String) Do Do While Len(a) <> Len(b) If Len(a) > Len(b) Then b = "0" & b Else a = "0" & a End If Loop a = a: b = b If a > b Then a = Bless(a, b) If b > a Then b = Bless(b, a) ta = a: tb = b Do While Left(ta, 1) = 0 Or Left(tb, 1) = 0 If Left(ta, 1) = 0 Then ta = Right(ta, Len(ta) - 1) If Left(tb, 1) = 0 Then tb = Right(tb, Len(tb) - 1) Loop Loop While ta <> tb Divi = ta End Function Function Bless(ByVal A1 As String, ByVal A2 As String) Dim gets2 As Integer, ANS As String, NewANS As String '補齊 Do Until Len(A1) = Len(A2) If Len(A1) > Len(A2) Then A2 = "0" & A2 Else A1 = "0" & A1 End If Loop ' 9'S 10'S A2 = Tr9(A2) '計算 ANS = Bplus(A1, A2)
If Len(A2) < Len(ANS) Then '正數 ANS = Right(ANS, Len(ANS) - 1) Do While Left(ANS, 1) = 0 ANS = Right(ANS, Len(ANS) - 1) Loop Else
'負數 ANS = Tr9(ANS)
Do While Left(ANS, 1) = 0 ANS = Right(ANS, Len(ANS) - 1) Loop
ANS = "-" & ANS End If
Bless = ANS End Function Function Bplus(ByVal N1 As String, ByVal N2 As String)
Dim NS1 As Integer, NS2 As Integer, Tmp As Integer, Plus As String, FAns As String
Do Until Len(N1) = Len(N2) If Len(N1) > Len(N2) Then N2 = "0" & N2 Else N1 = "0" & N1 End If Loop
For i = Len(N1) To 0 Step -1
If i = 0 Then FAns = Tmp & FAns Else NS1 = Mid(N1, i, 1) NS2 = Mid(N2, i, 1) Plus = NS1 + NS2 FAns = ((Plus + Tmp) Mod 10) & FAns Tmp = (Plus + Tmp) \ 10 End If
Next
If Left(FAns, 1) = "0" Then FAns = Right(FAns, Len(FAns) - 1)
Bplus = FAns
End Function
Function Tr9(T As String) Dim NewT As String, gets As String ' 9'S For i = 1 To Len(T) gets = Mid(T, i, 1) NewT = NewT & (9 - gets) Next
Private Sub Form_Load() Dim A As String Dim B As String Dim c As String Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, A, B
If Len(A) > Len(B) Then B = MLEN(B, Len(A)) If Len(A) < Len(B) Then A = MLEN(A, Len(B))
Do While max(A, B) <> 2
If max(A, B) = 0 Then
D = Mplus(A, M9(B)) D = Right(D, Len(D) - 1)
i = 1 Do While Mid(D, i, 1) = "0" If Mid(D, i, 1) = "0" Then D = Replace(D, "0", "", 1, 1) i = i + 1 Loop
A = D
End If
If max(A, B) = 1 Then
D = Mplus(B, M9(A)) D = Right(D, Len(D) - 1)
i = 1 Do While Mid(D, i, 1) = "0" If Mid(D, i, 1) = "0" Then D = Replace(D, "0", "", 1, 1) i = i + 1 Loop
B = D
End If
If Len(A) > Len(B) Then B = MLEN(B, Len(A)) If Len(A) < Len(B) Then A = MLEN(A, Len(B))
Loop
Do While Left(A, 1) = "0" If Left(A, 1) = "0" Then A = Replace(A, "0", "", 1, 1) Loop Print #2, A
Close #2 Close #1
End End Sub
Function max(X, Y) '判斷大小 For i = 1 To Len(X) If Val(Mid(X, i, 1)) > Val(Mid(Y, i, 1)) Then max = 0: Exit For If Val(Mid(X, i, 1)) < Val(Mid(Y, i, 1)) Then max = 1: Exit For If i = Len(Y) Then max = 2: Exit For Next i End Function Function Mplus(X, Y) go = 0 ans = ""
For i = Len(X) To 1 Step -1 Z = Val(Mid(X, i, 1)) + Val(Mid(Y, i, 1)) + go If Z >= 10 Then go = Z \ 10: Z = Z Mod 10 Else go = 0 ans = Z & ans Next i If go <> 0 Then ans = go & ans Mplus = ans
End Function
Function MLEN(X, Y) '補0 X 為要補數 Y為字長數
For i = 1 To (Y - Len(X)) X = "0" & X Next i MLEN = X
End Function Function M9(Y) '先轉九再轉十 Z = "" For i = 1 To Len(Y) Z = Z & 9 - Val(Mid(Y, i, 1)) Next i
Private Sub Form_Load() Dim Mm As String, Nn As String Me.Hide Open App.Path & "\out.txt" For Output As #2 Open App.Path & "\in.txt" For Input As #1 Input #1, Mm, Nn Print #2, Ea(Mm, Nn) Close #1 Close #2 End End Sub Function Ea(ByVal a1, ByVal b1) Print a1, b1 If Len(a1) > Len(b1) Then Max = a1: Min = b1 If Len(b1) > Len(a1) Then Max = b1: Min = a1 If Len(a1) = Len(b1) Then If a1 > b1 Then Max = a1: Min = b1 Else Max = b1: Min = a1 End If Do While Max <> Min Max = se(Max, Min) If Len(Min) > Len(Max) Then Call cha(Min, Max) If Len(Max) = Len(Min) Then If Max < Min Then Call cha(Min, Max) Loop Ea = Max End Function Function se(ByVal a2, ByVal b2) If Len(a2) <> Len(b2) Then If Len(b2) > Len(a2) Then Call cz(b2, a2) Else Call cz(a2, b2) End If an = p(c(b2), 1) an = p(a2, an) an = Right(an, Len(an) - 1) Do If Left(an, 1) = 0 Then an = Right(an, Len(an) - 1) Else Exit Do End If Loop se = an End Function Sub cha(a0, b0) t0 = a0 a0 = b0 b0 = t0 End Sub Function c(a4) Lena4 = Len(a4) For i = 0 To Lena4 - 1 X4 = Mid(a4, Lena4 - i, 1) X4 = 9 - Val(X4) aa4 = X4 & aa4 Next i c = aa4 End Function Function p(m2, n2) Lenm2 = Len(m2) Lenn2 = Len(n2) Do Until i2 > Lenm2 And i2 > Lenn2 If Lenm2 > i2 Then X2 = Val(Mid(m2, Lenm2 - i2, 1)) Else X2 = 0 If Lenn2 > i2 Then Y2 = Val(Mid(n2, Lenn2 - i2, 1)) Else Y2 = 0 z2 = X2 + Y2 + c2 c2 = z2 \ 10 r2 = (z2 Mod 10) & r2 i2 = i2 + 1 If i2 > Lenm2 And i2 > Lenn2 And (z2 Mod 10 = 0) Then r2 = Right(r2, Len(r2) - 1) Loop p = r2 End Function Function cz(a3, b3) Do Until Len(b3) = Len(a3) b3 = "0" & b3 Loop End Function -------------- 有些數據要跑好久 有些幾秒就結束了0..0 in.txt 43175241382829794264 5484651872154782 out.txt 2
Dim ta, tb
回覆刪除Private Sub Form_Load()
Dim S1 As String, S2 As String
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, S1
Input #1, S2
SANS = Divi((S1), (S2))
Print #2, "最大公因數:" & SANS
End
End Sub
Function Divi(a As String, b As String)
Do
Do While Len(a) <> Len(b)
If Len(a) > Len(b) Then
b = "0" & b
Else
a = "0" & a
End If
Loop
a = a: b = b
If a > b Then a = Bless(a, b)
If b > a Then b = Bless(b, a)
ta = a: tb = b
Do While Left(ta, 1) = 0 Or Left(tb, 1) = 0
If Left(ta, 1) = 0 Then ta = Right(ta, Len(ta) - 1)
If Left(tb, 1) = 0 Then tb = Right(tb, Len(tb) - 1)
Loop
Loop While ta <> tb
Divi = ta
End Function
Function Bless(ByVal A1 As String, ByVal A2 As String)
Dim gets2 As Integer, ANS As String, NewANS As String
'補齊
Do Until Len(A1) = Len(A2)
If Len(A1) > Len(A2) Then
A2 = "0" & A2
Else
A1 = "0" & A1
End If
Loop
' 9'S 10'S
A2 = Tr9(A2)
'計算
ANS = Bplus(A1, A2)
If Len(A2) < Len(ANS) Then
'正數
ANS = Right(ANS, Len(ANS) - 1)
Do While Left(ANS, 1) = 0
ANS = Right(ANS, Len(ANS) - 1)
Loop
Else
'負數
ANS = Tr9(ANS)
Do While Left(ANS, 1) = 0
ANS = Right(ANS, Len(ANS) - 1)
Loop
ANS = "-" & ANS
End If
Bless = ANS
End Function
Function Bplus(ByVal N1 As String, ByVal N2 As String)
Dim NS1 As Integer, NS2 As Integer, Tmp As Integer, Plus As String, FAns As String
Do Until Len(N1) = Len(N2)
If Len(N1) > Len(N2) Then
N2 = "0" & N2
Else
N1 = "0" & N1
End If
Loop
For i = Len(N1) To 0 Step -1
If i = 0 Then
FAns = Tmp & FAns
Else
NS1 = Mid(N1, i, 1)
NS2 = Mid(N2, i, 1)
Plus = NS1 + NS2
FAns = ((Plus + Tmp) Mod 10) & FAns
Tmp = (Plus + Tmp) \ 10
End If
Next
If Left(FAns, 1) = "0" Then FAns = Right(FAns, Len(FAns) - 1)
Bplus = FAns
End Function
Function Tr9(T As String)
Dim NewT As String, gets As String
' 9'S
For i = 1 To Len(T)
gets = Mid(T, i, 1)
NewT = NewT & (9 - gets)
Next
' 10'S
Tr9 = Bplus(NewT, "1")
End Function
Private Sub Form_Load()
回覆刪除Dim A As String
Dim B As String
Dim c As String
Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, A, B
If Len(A) > Len(B) Then B = MLEN(B, Len(A))
If Len(A) < Len(B) Then A = MLEN(A, Len(B))
Do While max(A, B) <> 2
If max(A, B) = 0 Then
D = Mplus(A, M9(B))
D = Right(D, Len(D) - 1)
i = 1
Do While Mid(D, i, 1) = "0"
If Mid(D, i, 1) = "0" Then D = Replace(D, "0", "", 1, 1)
i = i + 1
Loop
A = D
End If
If max(A, B) = 1 Then
D = Mplus(B, M9(A))
D = Right(D, Len(D) - 1)
i = 1
Do While Mid(D, i, 1) = "0"
If Mid(D, i, 1) = "0" Then D = Replace(D, "0", "", 1, 1)
i = i + 1
Loop
B = D
End If
If Len(A) > Len(B) Then B = MLEN(B, Len(A))
If Len(A) < Len(B) Then A = MLEN(A, Len(B))
Loop
Do While Left(A, 1) = "0"
If Left(A, 1) = "0" Then A = Replace(A, "0", "", 1, 1)
Loop
Print #2, A
Close #2
Close #1
End
End Sub
Function max(X, Y) '判斷大小
For i = 1 To Len(X)
If Val(Mid(X, i, 1)) > Val(Mid(Y, i, 1)) Then max = 0: Exit For
If Val(Mid(X, i, 1)) < Val(Mid(Y, i, 1)) Then max = 1: Exit For
If i = Len(Y) Then max = 2: Exit For
Next i
End Function
Function Mplus(X, Y)
go = 0
ans = ""
For i = Len(X) To 1 Step -1
Z = Val(Mid(X, i, 1)) + Val(Mid(Y, i, 1)) + go
If Z >= 10 Then go = Z \ 10: Z = Z Mod 10 Else go = 0
ans = Z & ans
Next i
If go <> 0 Then ans = go & ans
Mplus = ans
End Function
Function MLEN(X, Y) '補0 X 為要補數 Y為字長數
For i = 1 To (Y - Len(X))
X = "0" & X
Next i
MLEN = X
End Function
Function M9(Y) '先轉九再轉十
Z = ""
For i = 1 To Len(Y)
Z = Z & 9 - Val(Mid(Y, i, 1))
Next i
V = 1
V = MLEN(V, Len(Z))
M9 = Mplus(Z, V)
End Function
Private Sub Form_Load()
回覆刪除Dim Mm As String, Nn As String
Me.Hide
Open App.Path & "\out.txt" For Output As #2
Open App.Path & "\in.txt" For Input As #1
Input #1, Mm, Nn
Print #2, Ea(Mm, Nn)
Close #1
Close #2
End
End Sub
Function Ea(ByVal a1, ByVal b1)
Print a1, b1
If Len(a1) > Len(b1) Then Max = a1: Min = b1
If Len(b1) > Len(a1) Then Max = b1: Min = a1
If Len(a1) = Len(b1) Then
If a1 > b1 Then Max = a1: Min = b1 Else Max = b1: Min = a1
End If
Do While Max <> Min
Max = se(Max, Min)
If Len(Min) > Len(Max) Then Call cha(Min, Max)
If Len(Max) = Len(Min) Then If Max < Min Then Call cha(Min, Max)
Loop
Ea = Max
End Function
Function se(ByVal a2, ByVal b2)
If Len(a2) <> Len(b2) Then
If Len(b2) > Len(a2) Then Call cz(b2, a2) Else Call cz(a2, b2)
End If
an = p(c(b2), 1)
an = p(a2, an)
an = Right(an, Len(an) - 1)
Do
If Left(an, 1) = 0 Then
an = Right(an, Len(an) - 1)
Else
Exit Do
End If
Loop
se = an
End Function
Sub cha(a0, b0)
t0 = a0
a0 = b0
b0 = t0
End Sub
Function c(a4)
Lena4 = Len(a4)
For i = 0 To Lena4 - 1
X4 = Mid(a4, Lena4 - i, 1)
X4 = 9 - Val(X4)
aa4 = X4 & aa4
Next i
c = aa4
End Function
Function p(m2, n2)
Lenm2 = Len(m2)
Lenn2 = Len(n2)
Do Until i2 > Lenm2 And i2 > Lenn2
If Lenm2 > i2 Then X2 = Val(Mid(m2, Lenm2 - i2, 1)) Else X2 = 0
If Lenn2 > i2 Then Y2 = Val(Mid(n2, Lenn2 - i2, 1)) Else Y2 = 0
z2 = X2 + Y2 + c2
c2 = z2 \ 10
r2 = (z2 Mod 10) & r2
i2 = i2 + 1
If i2 > Lenm2 And i2 > Lenn2 And (z2 Mod 10 = 0) Then r2 = Right(r2, Len(r2) - 1)
Loop
p = r2
End Function
Function cz(a3, b3)
Do Until Len(b3) = Len(a3)
b3 = "0" & b3
Loop
End Function
--------------
有些數據要跑好久 有些幾秒就結束了0..0
in.txt
43175241382829794264
5484651872154782
out.txt
2