我想看用"遞迴"和"非遞迴"方式求出N個正整數的最大公因數.最小公倍數.N個正整數的數列和以及N個正整數由小到大之排列
INPUT:任意輸入N個正整數(2<=N<=20)
OUUPUT:N個正整數的最大公因數.最小公倍數.N個正整數的數列和以及N個正整數由小到大之排列順序
寫出來附加註解(更好)讓我了解...3Q
2005-12-16 15:37:58 · 2 個解答 · 發問者 失魂程式 2 in 電腦與網際網路 ➔ 程式設計
請問一下您是用vb中的哪一種專案類型中的哪一種範本寫的
2005-12-19 13:53:26 · update #1
可以把整個專案寄給我看嗎...謝謝
favofavofavo@yahoo.com.tw
2005-12-19 13:58:21 · update #2
DoEvents()和Cls()有問題...他說未宣告
我讓這兩行變綠色字...還是有問題
一定要打到20個字他才會執行...不能只輸入3個嗎
而且結果都跑不出來...他寫從字串"數列和:XX"轉成型別'INTEGER'是無效的
2005-12-19 15:53:15 · update #3
您寄給我可能會比較清楚哪裏有問題...謝
我的是.NET2003版本
2005-12-19 15:56:51 · update #4
'打開vb做一個標準執行檔,在表單上設置一個CommandButton再把以下的Code貼上再執行就可!
Dim A() As Variant, X As Integer
Private Sub Command1_Click()
Dim S As String, L As Integer, M As Integer, I As Integer, J As Integer, T As Double, Tmp As Integer
Erase A: X = 0: T = 0
Do
S = InputBox("請輸入2~20個正整數,已輸入:" & X & "個數字")
If IsNumeric(S) Then
If Val(S) > 0 And Val(S) < 32768 Then
ReDim Preserve A(X): A(X) = Int(S)
T = T + Int(S): X = X + 1
End If
End If
Loop Until S = "0" Or X > 19
X = X - 1: If X < 1 Then Exit Sub
For I = 0 To X
For J = 0 To X
If A(I) < A(J) Then Tmp = A(I): A(I) = A(J): A(J) = Tmp
Next
Next
Cls
Print "數列和:" & T
Print "最大公因數:" & CAL
Print "最小公倍數:" & CAM
Print "排列順序:" & Join(A, ",")
End Sub
Function CAL() As Integer '最大公因數非遞迴
Dim I As Integer, J As Integer, B As Boolean
For I = A(0) To 1 Step -1
B = True
For J = 0 To X
If A(J) Mod I <> 0 Then B = False: Exit For
Next
If B Then CAL = I: Exit For
Next
End Function
'兩數相乘再除以兩數之最大公因數→兩數之最小公倍數,以 2,4,8為例:2,4最大公因數 2 ,2*4/2=4兩數之最小公倍數 4 ; 4,8最大公因數 4 ,4*8/4=8兩數之最小公倍數 8 ,所以 2,4,8最小公倍數→8
Function CAM() As Variant '最小公倍數非遞迴
Dim I As Integer, J As Integer, B As Boolean, L As Variant, M As Integer, K As Integer, Tmp As Integer
K = 1: M = A(0): L = A(1)
Do
If K > 1 Then M = A(K)
If M > L Then Tmp = M: M = L: L = Tmp
For I = M To 1 Step -1
If M Mod I = 0 Then
If InStr(CDec(L / I), ".") = 0 Then L = CDec(L / I * M): Exit For
'此處也可寫成 If L Mod I = 0 ,不過若L超過長整數則會掛掉!If InStr(CDec(L / I), ".") = 0 ,能讓L值大一點,但若大過79,228,162,514,264,337,593,543,950,335時程式一樣失效!!故建議輸入的值別太大,否則就必須另寫一個處理 L/I 跟 L*M/I 的程式!
End If
Next
K = K + 1
DoEvents
Loop Until K > X
CAM = L
End Function
2005-12-20 16:55:37 補充:
我是用VB寫的,不是VB.net難怪你跑不出來!!
2005-12-19 13:03:20 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋
該如何修改?
2005-12-22 16:35:40 · answer #2 · answered by Anonymous · 0⤊ 0⤋