詳細的題目在這裡!! > quietmes.myweb.hinet.net/week.doc <
將真分數分解為分子為1之分數和。
範例輸入:
2
17 21
18 23
範例輸出:
2 4 17 1428
2 4 31 2852
2006-10-26 06:24:10 · 5 個解答 · 發問者 小均 2 in 電腦與網際網路 ➔ 程式設計
Private Sub Command1_Click() Print "17/21 = "; Cal(17, 21) Print "18/23 = "; Cal(18, 23)End SubFunction Cal(ByVal S1 As Integer, ByVal S2 As Integer) As String Dim P As Long, A As Long, M As Long, N As Long Do P = S2 \ S1: M = S2 Mod S1 If M Then P = P + 1 Cal = Cal & "1/" & P & " + " A = GCM(S2, P) '最大公倍數 M = (A \ S2) * S1 N = A \ P: S1 = M - N '計算分子 S2 = A '分母 Else Cal = Cal & "1/" & P: Exit Do End If LoopEnd Function'最小公因數Function GCD(ByVal S1 As Long, ByVal S2 As Long) As Long Dim T As Long Do While S1 Mod S2 T = S1 Mod S2: S1 = S2: S2 = T Loop GCD = S2End Function'最大公倍數Function GCM(ByVal S1 As Long, ByVal S2 As Long) As Variant GCM = CDec(S1 * S2 / GCD(S1, S2))End Function有些數字計算起來後分母會變的非常大,使用一般的+-*/ Mod \是無法計算的,以下連結是我之前曾經以大數的+-*/ Mod \來寫此題,有興趣的大大可參考看看。YSI 空間(只能放7天):點我下載ZUpLoad 空間(放多久..不知道,進入後找到Download file下載):進入下載
2006-10-27 09:12:42 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋
這招我也用過阿!!
可是出題的老師出了一個值
結果答案錯了~~
就想說有沒有比較正統的方法!
2006-10-27 06:33:40 · answer #2 · answered by 小均 2 · 0⤊ 0⤋
原來looping是IRA大大@@..
剛才才發現
2006-10-26 09:35:36 · answer #3 · answered by ? 6 · 0⤊ 0⤋
為什麼範例輸入的那兩個真分數,怎麼剛好都是由4個1/x來組成,說不定有5個1/x來組成?
2006-10-26 07:56:41 · answer #4 · answered by Almond 6 · 0⤊ 0⤋
想不想聽一種作弊方法...因為它沒有限制什麼.
所以
17 21
可為
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21
嗯....(真的作弊太嚴重,也沒限定說有什麼條件...)
不過這題目蠻有趣.可以想想...
2006-10-26 11:37:36 補充:
17 21
3 3 7 也可以
21->3*7
1/3+1/7=10/21還差7/21->1/3
17/21=1/3+1/3+1/7
2006-10-26 11:42:59 補充:
它的計算方式是減最接近的1/x然後一直減下去.
2006-10-26 11:49:52 補充:
方案一...顯然不是很好...
Private Sub Command1_Click()
Dim x, y, val
x = 18
y = 23
val = x / y
For i = 2 To 10000
If val >= 1 / i Then
val = val - 1 / i
Print i
End If
If Abs(val) <= 0.00000001 Then
i = 10001
End If
Next i
End Sub
2006-10-26 12:10:13 補充:
根據觀察...那好像是用一種Greedy策略產生...
(22 / 2 311) - (1 / 106) = 8.57261824 × 10-5
這種策略也許會很多個...
像這只有3個
19/33 = (1 / 2) + (1 / 14) + (1 / 231)
2006-10-26 18:38:54 補充:
被發現了...
不過愁大的VB答題資料對我多少有幫助.
2006-10-26 07:29:41 · answer #5 · answered by ? 4 · 0⤊ 0⤋